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> |
| 33 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 34 | #include "TemporaryFile.h" |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 35 | |
| 36 | #define ASSERT_DL_NOTNULL(ptr) \ |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 37 | ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror() |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 38 | |
| 39 | #define ASSERT_DL_ZERO(i) \ |
| 40 | ASSERT_EQ(0, i) << "dlerror: " << dlerror() |
| 41 | |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 42 | #define ASSERT_NOERROR(i) \ |
| 43 | ASSERT_NE(-1, i) << "errno: " << strerror(errno) |
| 44 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 45 | #define ASSERT_SUBSTR(needle, haystack) \ |
| 46 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) |
| 47 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 48 | |
| 49 | typedef int (*fn)(void); |
| 50 | #define LIBNAME "libdlext_test.so" |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 51 | #define LIBNAME_NORELRO "libdlext_test_norelro.so" |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 52 | #define LIBSIZE 1024*1024 // how much address space to reserve for it |
| 53 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 54 | #if defined(__LP64__) |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 55 | #define NATIVE_TESTS_PATH "/nativetest64" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 56 | #else |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 57 | #define NATIVE_TESTS_PATH "/nativetest" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 58 | #endif |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 59 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 60 | #define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so" |
| 61 | #define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip" |
| 62 | #define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip" |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 63 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 64 | #define LIBZIP_OFFSET PAGE_SIZE |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 65 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 66 | class DlExtTest : public ::testing::Test { |
| 67 | protected: |
| 68 | virtual void SetUp() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 69 | handle_ = nullptr; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 70 | // verify that we don't have the library loaded already |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 71 | void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD); |
| 72 | ASSERT_TRUE(h == nullptr); |
| 73 | h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD); |
| 74 | ASSERT_TRUE(h == nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 75 | // call dlerror() to swallow the error, and check it was the one we wanted |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 76 | 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] | 77 | } |
| 78 | |
| 79 | virtual void TearDown() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 80 | if (handle_ != nullptr) { |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 81 | ASSERT_DL_ZERO(dlclose(handle_)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void* handle_; |
| 86 | }; |
| 87 | |
| 88 | TEST_F(DlExtTest, ExtInfoNull) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 89 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 90 | ASSERT_DL_NOTNULL(handle_); |
| 91 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 92 | ASSERT_DL_NOTNULL(f); |
| 93 | EXPECT_EQ(4, f()); |
| 94 | } |
| 95 | |
| 96 | TEST_F(DlExtTest, ExtInfoNoFlags) { |
| 97 | android_dlextinfo extinfo; |
| 98 | extinfo.flags = 0; |
| 99 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 100 | ASSERT_DL_NOTNULL(handle_); |
| 101 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 102 | ASSERT_DL_NOTNULL(f); |
| 103 | EXPECT_EQ(4, f()); |
| 104 | } |
| 105 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 106 | TEST_F(DlExtTest, ExtInfoUseFd) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 107 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH; |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 108 | |
| 109 | android_dlextinfo extinfo; |
| 110 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 111 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(extinfo.library_fd != -1); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 113 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 114 | ASSERT_DL_NOTNULL(handle_); |
| 115 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 116 | ASSERT_DL_NOTNULL(f); |
| 117 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 118 | |
| 119 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 120 | ASSERT_DL_NOTNULL(taxicab_number); |
| 121 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 124 | TEST_F(DlExtTest, ExtInfoUseFdWithOffset) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 125 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 126 | |
| 127 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 128 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 129 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 130 | extinfo.library_fd_offset = LIBZIP_OFFSET; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 131 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 132 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 133 | ASSERT_DL_NOTNULL(handle_); |
| 134 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 135 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 136 | ASSERT_DL_NOTNULL(taxicab_number); |
| 137 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 141 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 142 | // lib_path is relative when $ANDROID_DATA is relative |
| 143 | char lib_realpath_buf[PATH_MAX]; |
| 144 | ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf); |
| 145 | const std::string lib_realpath = std::string(lib_realpath_buf); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 146 | |
| 147 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 148 | 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] | 149 | 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] | 150 | extinfo.library_fd_offset = 17; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 151 | |
| 152 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 153 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 154 | ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror()); |
| 155 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 156 | // Test an address above 2^44, for http://b/18178121 . |
| 157 | extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 158 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 159 | ASSERT_TRUE(handle_ == nullptr); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 160 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror()); |
| 161 | |
| 162 | extinfo.library_fd_offset = 0LL - PAGE_SIZE; |
| 163 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 164 | ASSERT_TRUE(handle_ == nullptr); |
| 165 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror()); |
| 166 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 167 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 168 | handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 169 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 170 | ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror()); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 171 | |
Dmitriy Ivanov | fd7a91e | 2015-11-06 10:44:37 -0800 | [diff] [blame] | 172 | // Check if dlsym works after unsuccessful dlopen(). |
| 173 | // Supply non-exiting one to make linker visit every soinfo. |
| 174 | void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___"); |
| 175 | ASSERT_TRUE(sym == nullptr); |
| 176 | |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 177 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) { |
| 181 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 182 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
| 183 | extinfo.library_fd_offset = LIBZIP_OFFSET; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 184 | |
| 185 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 186 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 187 | 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] | 188 | } |
| 189 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 190 | TEST(dlext, android_dlopen_ext_force_load_smoke) { |
| 191 | // 1. Open actual file |
| 192 | void* handle = dlopen("libdlext_test.so", RTLD_NOW); |
| 193 | ASSERT_DL_NOTNULL(handle); |
| 194 | // 2. Open link with force_load flag set |
| 195 | android_dlextinfo extinfo; |
| 196 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 197 | void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo); |
| 198 | ASSERT_DL_NOTNULL(handle2); |
| 199 | ASSERT_TRUE(handle != handle2); |
| 200 | |
| 201 | dlclose(handle2); |
| 202 | dlclose(handle); |
| 203 | } |
| 204 | |
| 205 | TEST(dlext, android_dlopen_ext_force_load_soname_exception) { |
| 206 | // Check if soname lookup still returns already loaded library |
| 207 | // when ANDROID_DLEXT_FORCE_LOAD flag is specified. |
| 208 | void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW); |
| 209 | ASSERT_DL_NOTNULL(handle); |
| 210 | |
| 211 | android_dlextinfo extinfo; |
| 212 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 213 | |
| 214 | // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so |
| 215 | void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); |
| 216 | |
| 217 | ASSERT_DL_NOTNULL(handle2); |
| 218 | ASSERT_TRUE(handle == handle2); |
| 219 | |
| 220 | dlclose(handle2); |
| 221 | dlclose(handle); |
| 222 | } |
| 223 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 224 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
| 225 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
| 226 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 227 | 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] | 228 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 229 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 230 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 231 | ASSERT_DL_NOTNULL(taxicab_number); |
| 232 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 233 | |
| 234 | dlclose(handle); |
| 235 | } |
| 236 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 237 | TEST(dlfcn, dlopen_from_zip_with_dt_runpath) { |
| 238 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH; |
| 239 | |
| 240 | void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW); |
| 241 | |
| 242 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 243 | |
| 244 | typedef void *(* dlopen_b_fn)(); |
| 245 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 246 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 247 | |
| 248 | void *p = fn(); |
| 249 | ASSERT_TRUE(p != nullptr) << dlerror(); |
| 250 | |
| 251 | dlclose(p); |
| 252 | dlclose(handle); |
| 253 | } |
| 254 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 255 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 256 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir"; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 257 | |
| 258 | typedef void (*fn_t)(const char*); |
| 259 | fn_t android_update_LD_LIBRARY_PATH = |
| 260 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 261 | |
| 262 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 263 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 264 | void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 265 | ASSERT_TRUE(handle == nullptr); |
| 266 | |
| 267 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 268 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 269 | handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 270 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 271 | |
| 272 | int (*fn)(void); |
| 273 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 274 | ASSERT_TRUE(fn != nullptr); |
| 275 | EXPECT_EQ(4, fn()); |
| 276 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 277 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 278 | ASSERT_DL_NOTNULL(taxicab_number); |
| 279 | EXPECT_EQ(1729U, *taxicab_number); |
| 280 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 281 | dlclose(handle); |
| 282 | } |
| 283 | |
| 284 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 285 | TEST_F(DlExtTest, Reserved) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 286 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 287 | -1, 0); |
| 288 | ASSERT_TRUE(start != MAP_FAILED); |
| 289 | android_dlextinfo extinfo; |
| 290 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 291 | extinfo.reserved_addr = start; |
| 292 | extinfo.reserved_size = LIBSIZE; |
| 293 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 294 | ASSERT_DL_NOTNULL(handle_); |
| 295 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 296 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 297 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 298 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 299 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 300 | EXPECT_EQ(4, f()); |
| 301 | } |
| 302 | |
| 303 | TEST_F(DlExtTest, ReservedTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 304 | void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 305 | -1, 0); |
| 306 | ASSERT_TRUE(start != MAP_FAILED); |
| 307 | android_dlextinfo extinfo; |
| 308 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 309 | extinfo.reserved_addr = start; |
| 310 | extinfo.reserved_size = PAGE_SIZE; |
| 311 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 312 | EXPECT_EQ(nullptr, handle_); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST_F(DlExtTest, ReservedHint) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 316 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 317 | -1, 0); |
| 318 | ASSERT_TRUE(start != MAP_FAILED); |
| 319 | android_dlextinfo extinfo; |
| 320 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 321 | extinfo.reserved_addr = start; |
| 322 | extinfo.reserved_size = LIBSIZE; |
| 323 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 324 | ASSERT_DL_NOTNULL(handle_); |
| 325 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 326 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 327 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 328 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 329 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 330 | EXPECT_EQ(4, f()); |
| 331 | } |
| 332 | |
| 333 | TEST_F(DlExtTest, ReservedHintTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 334 | void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 335 | -1, 0); |
| 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 = PAGE_SIZE; |
| 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_TRUE(reinterpret_cast<void*>(f) < start || |
| 346 | (reinterpret_cast<void*>(f) >= |
| 347 | reinterpret_cast<char*>(start) + PAGE_SIZE)); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 348 | EXPECT_EQ(4, f()); |
| 349 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 350 | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 351 | TEST_F(DlExtTest, LoadAtFixedAddress) { |
| 352 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 353 | -1, 0); |
| 354 | ASSERT_TRUE(start != MAP_FAILED); |
| 355 | munmap(start, LIBSIZE); |
| 356 | |
| 357 | android_dlextinfo extinfo; |
| 358 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 359 | extinfo.reserved_addr = start; |
| 360 | |
| 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); |
| 365 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
| 366 | EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE); |
| 367 | |
| 368 | EXPECT_EQ(4, f()); |
| 369 | } |
| 370 | |
| 371 | TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) { |
| 372 | void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE, |
| 373 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 374 | ASSERT_TRUE(start != MAP_FAILED); |
| 375 | munmap(start, LIBSIZE + PAGE_SIZE); |
| 376 | void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE, |
| 377 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 378 | ASSERT_TRUE(new_addr != MAP_FAILED); |
| 379 | |
| 380 | android_dlextinfo extinfo; |
| 381 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 382 | extinfo.reserved_addr = start; |
| 383 | |
| 384 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 385 | ASSERT_TRUE(handle_ == nullptr); |
| 386 | } |
| 387 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 388 | class DlExtRelroSharingTest : public DlExtTest { |
| 389 | protected: |
| 390 | virtual void SetUp() { |
| 391 | DlExtTest::SetUp(); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 392 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 393 | -1, 0); |
| 394 | ASSERT_TRUE(start != MAP_FAILED); |
| 395 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 396 | extinfo_.reserved_addr = start; |
| 397 | extinfo_.reserved_size = LIBSIZE; |
| 398 | extinfo_.relro_fd = -1; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 401 | virtual void TearDown() { |
| 402 | DlExtTest::TearDown(); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 403 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 404 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 405 | void CreateRelroFile(const char* lib, const char* relro_file) { |
| 406 | int relro_fd = open(relro_file, O_RDWR | O_TRUNC); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 407 | ASSERT_NOERROR(relro_fd); |
| 408 | |
| 409 | pid_t pid = fork(); |
| 410 | if (pid == 0) { |
| 411 | // child process |
| 412 | extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO; |
| 413 | extinfo_.relro_fd = relro_fd; |
| 414 | void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 415 | if (handle == nullptr) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 416 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 417 | exit(1); |
| 418 | } |
| 419 | exit(0); |
| 420 | } |
| 421 | |
| 422 | // continuing in parent |
| 423 | ASSERT_NOERROR(close(relro_fd)); |
| 424 | ASSERT_NOERROR(pid); |
| 425 | int status; |
| 426 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 427 | ASSERT_TRUE(WIFEXITED(status)); |
| 428 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 429 | |
| 430 | // reopen file for reading so it can be used |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 431 | relro_fd = open(relro_file, O_RDONLY); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 432 | ASSERT_NOERROR(relro_fd); |
| 433 | extinfo_.flags |= ANDROID_DLEXT_USE_RELRO; |
| 434 | extinfo_.relro_fd = relro_fd; |
| 435 | } |
| 436 | |
| 437 | void TryUsingRelro(const char* lib) { |
| 438 | handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 439 | ASSERT_DL_NOTNULL(handle_); |
| 440 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 441 | ASSERT_DL_NOTNULL(f); |
| 442 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 443 | |
| 444 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 445 | ASSERT_DL_NOTNULL(taxicab_number); |
| 446 | EXPECT_EQ(1729U, *taxicab_number); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 449 | void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out); |
| 450 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 451 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 455 | TemporaryFile tf; // Use tf to get an unique filename. |
| 456 | ASSERT_NOERROR(close(tf.fd)); |
| 457 | |
| 458 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 459 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 460 | |
| 461 | // Use destructor of tf to close and unlink the file. |
| 462 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 466 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 467 | ASSERT_NOERROR(close(tf.fd)); |
| 468 | |
| 469 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 470 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 471 | |
| 472 | // Use destructor of tf to close and unlink the file. |
| 473 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 477 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 478 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 479 | |
| 480 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 481 | if (geteuid() != 0) { |
| 482 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 483 | return; |
| 484 | } |
| 485 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 486 | TemporaryFile tf; // Use tf to get an unique filename. |
| 487 | ASSERT_NOERROR(close(tf.fd)); |
| 488 | |
| 489 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
| 490 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 491 | int pipefd[2]; |
| 492 | ASSERT_NOERROR(pipe(pipefd)); |
| 493 | |
| 494 | size_t without_sharing, with_sharing; |
| 495 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing)); |
| 496 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing)); |
| 497 | |
| 498 | // We expect the sharing to save at least 10% of the total PSS. In practice |
| 499 | // it saves 40%+ for this test. |
| 500 | size_t expected_size = without_sharing - (without_sharing/10); |
| 501 | EXPECT_LT(with_sharing, expected_size); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 502 | |
| 503 | // Use destructor of tf to close and unlink the file. |
| 504 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void getPss(pid_t pid, size_t* pss_out) { |
| 508 | pm_kernel_t* kernel; |
| 509 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 510 | |
| 511 | pm_process_t* process; |
| 512 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 513 | |
| 514 | pm_map_t** maps; |
| 515 | size_t num_maps; |
| 516 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 517 | |
| 518 | size_t total_pss = 0; |
| 519 | for (size_t i = 0; i < num_maps; i++) { |
| 520 | pm_memusage_t usage; |
| 521 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 522 | total_pss += usage.pss; |
| 523 | } |
| 524 | *pss_out = total_pss; |
| 525 | |
| 526 | free(maps); |
| 527 | pm_process_destroy(process); |
| 528 | pm_kernel_destroy(kernel); |
| 529 | } |
| 530 | |
| 531 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, |
| 532 | size_t* pss_out) { |
| 533 | const int CHILDREN = 20; |
| 534 | |
| 535 | // Create children |
| 536 | pid_t childpid[CHILDREN]; |
| 537 | int childpipe[CHILDREN]; |
| 538 | for (int i=0; i<CHILDREN; ++i) { |
| 539 | char read_buf; |
| 540 | int child_done_pipe[2], parent_done_pipe[2]; |
| 541 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 542 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 543 | |
| 544 | pid_t child = fork(); |
| 545 | if (child == 0) { |
| 546 | // close the 'wrong' ends of the pipes in the child |
| 547 | close(child_done_pipe[0]); |
| 548 | close(parent_done_pipe[1]); |
| 549 | |
| 550 | // open the library |
| 551 | void* handle; |
| 552 | if (share_relro) { |
| 553 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 554 | } else { |
| 555 | handle = dlopen(lib, RTLD_NOW); |
| 556 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 557 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 558 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 559 | exit(1); |
| 560 | } |
| 561 | |
| 562 | // close write end of child_done_pipe to signal the parent that we're done. |
| 563 | close(child_done_pipe[1]); |
| 564 | |
| 565 | // wait for the parent to close parent_done_pipe, then exit |
| 566 | read(parent_done_pipe[0], &read_buf, 1); |
| 567 | exit(0); |
| 568 | } |
| 569 | |
| 570 | ASSERT_NOERROR(child); |
| 571 | |
| 572 | // close the 'wrong' ends of the pipes in the parent |
| 573 | close(child_done_pipe[1]); |
| 574 | close(parent_done_pipe[0]); |
| 575 | |
| 576 | // wait for the child to be done |
| 577 | read(child_done_pipe[0], &read_buf, 1); |
| 578 | close(child_done_pipe[0]); |
| 579 | |
| 580 | // save the child's pid and the parent_done_pipe |
| 581 | childpid[i] = child; |
| 582 | childpipe[i] = parent_done_pipe[1]; |
| 583 | } |
| 584 | |
| 585 | // Sum the PSS of all the children |
| 586 | size_t total_pss = 0; |
| 587 | for (int i=0; i<CHILDREN; ++i) { |
| 588 | size_t child_pss; |
| 589 | ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss)); |
| 590 | total_pss += child_pss; |
| 591 | } |
| 592 | *pss_out = total_pss; |
| 593 | |
| 594 | // Close pipes and wait for children to exit |
| 595 | for (int i=0; i<CHILDREN; ++i) { |
| 596 | ASSERT_NOERROR(close(childpipe[i])); |
| 597 | } |
| 598 | for (int i=0; i<CHILDREN; ++i) { |
| 599 | int status; |
| 600 | ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0)); |
| 601 | ASSERT_TRUE(WIFEXITED(status)); |
| 602 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 603 | } |
| 604 | } |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 605 | |
| 606 | // Testing namespaces |
| 607 | static const char* g_public_lib = "libnstest_public.so"; |
| 608 | |
| 609 | TEST(dlext, ns_smoke) { |
| 610 | static const char* root_lib = "libnstest_root.so"; |
| 611 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 612 | |
| 613 | ASSERT_FALSE(android_init_public_namespace(path.c_str())); |
| 614 | ASSERT_STREQ("android_init_public_namespace failed: Error initializing public namespace: " |
| 615 | "\"libnstest_public.so\" was not found in the default namespace", dlerror()); |
| 616 | |
| 617 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 618 | |
| 619 | void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW); |
| 620 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 621 | |
| 622 | ASSERT_TRUE(android_init_public_namespace(path.c_str())) << dlerror(); |
| 623 | |
| 624 | // Check that libraries added to public namespace are NODELETE |
| 625 | dlclose(handle_public); |
| 626 | handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW | RTLD_NOLOAD); |
| 627 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 628 | |
| 629 | android_namespace_t* ns1 = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false); |
| 630 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
| 631 | |
| 632 | android_namespace_t* ns2 = android_create_namespace("private_isolated", nullptr, (lib_path + "/private_namespace_libs").c_str(), true); |
| 633 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
| 634 | |
| 635 | // This should not have affect search path for default namespace: |
| 636 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 637 | void* handle = dlopen(g_public_lib, RTLD_NOW); |
| 638 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 639 | dlclose(handle); |
| 640 | |
| 641 | android_dlextinfo extinfo; |
| 642 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 643 | extinfo.library_namespace = ns1; |
| 644 | |
| 645 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 646 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 647 | |
| 648 | extinfo.library_namespace = ns2; |
| 649 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 650 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 651 | |
| 652 | ASSERT_TRUE(handle1 != handle2); |
| 653 | |
| 654 | typedef const char* (*fn_t)(); |
| 655 | |
| 656 | fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 657 | ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror(); |
| 658 | fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 659 | ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror(); |
| 660 | |
| 661 | EXPECT_STREQ("This string is local to root library", ns_get_local_string1()); |
| 662 | EXPECT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 663 | |
| 664 | ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2()); |
| 665 | |
| 666 | fn_t ns_get_private_extern_string1 = |
| 667 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 668 | ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror(); |
| 669 | fn_t ns_get_private_extern_string2 = |
| 670 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 671 | ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror(); |
| 672 | |
| 673 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1()); |
| 674 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 675 | |
| 676 | ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2()); |
| 677 | |
| 678 | fn_t ns_get_public_extern_string1 = |
| 679 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 680 | ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror(); |
| 681 | fn_t ns_get_public_extern_string2 = |
| 682 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 683 | ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror(); |
| 684 | |
| 685 | EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1()); |
| 686 | ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2()); |
| 687 | |
| 688 | // and now check that dlopen() does the right thing in terms of preserving namespace |
| 689 | fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 690 | ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror(); |
| 691 | fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 692 | ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror(); |
| 693 | |
| 694 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1()); |
| 695 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 696 | |
| 697 | ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2()); |
| 698 | |
| 699 | dlclose(handle1); |
| 700 | |
| 701 | // Check if handle2 is still alive (and well) |
| 702 | ASSERT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 703 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 704 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2()); |
| 705 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 706 | |
| 707 | dlclose(handle2); |
| 708 | } |
| 709 | |
Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 710 | extern "C" void android_set_application_target_sdk_version(uint32_t target); |
| 711 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 712 | TEST(dlext, ns_isolated) { |
| 713 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 714 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 715 | |
| 716 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 717 | void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW); |
| 718 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 719 | |
Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 720 | android_set_application_target_sdk_version(42U); // something > 23 |
| 721 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 722 | ASSERT_TRUE(android_init_public_namespace(path.c_str())) << dlerror(); |
| 723 | |
| 724 | android_namespace_t* ns_not_isolated = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false); |
| 725 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
| 726 | |
| 727 | android_namespace_t* ns_isolated = android_create_namespace("private_isolated1", nullptr, (lib_path + "/private_namespace_libs").c_str(), true); |
| 728 | ASSERT_TRUE(ns_isolated != nullptr) << dlerror(); |
| 729 | |
| 730 | android_namespace_t* ns_isolated2 = android_create_namespace("private_isolated2", (lib_path + "/private_namespace_libs").c_str(), nullptr, true); |
| 731 | ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror(); |
| 732 | |
| 733 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 734 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 735 | |
| 736 | std::string lib_private_external_path = |
| 737 | lib_path + "/private_namespace_libs_external/libnstest_private_external.so"; |
| 738 | |
| 739 | // Load lib_private_external_path to default namespace |
| 740 | // (it should remain invisible for the isolated namespaces after this) |
| 741 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 742 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 743 | |
| 744 | android_dlextinfo extinfo; |
| 745 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 746 | extinfo.library_namespace = ns_not_isolated; |
| 747 | |
| 748 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 749 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 750 | |
| 751 | extinfo.library_namespace = ns_isolated; |
| 752 | |
| 753 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 754 | ASSERT_TRUE(handle2 == nullptr); |
| 755 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 756 | |
| 757 | // Check dlopen by absolute path |
| 758 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 759 | ASSERT_TRUE(handle2 == nullptr); |
| 760 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror()); |
| 761 | |
| 762 | extinfo.library_namespace = ns_isolated2; |
| 763 | |
| 764 | handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 765 | ASSERT_TRUE(handle2 == nullptr); |
| 766 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 767 | |
| 768 | // Check dlopen by absolute path |
| 769 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 770 | ASSERT_TRUE(handle2 == nullptr); |
| 771 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror()); |
| 772 | |
| 773 | typedef const char* (*fn_t)(); |
| 774 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 775 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 776 | |
| 777 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 778 | |
| 779 | fn_t ns_get_private_extern_string = |
| 780 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 781 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 782 | |
| 783 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 784 | |
| 785 | fn_t ns_get_public_extern_string = |
| 786 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 787 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 788 | |
| 789 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 790 | |
| 791 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 792 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 793 | |
| 794 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 795 | |
| 796 | dlclose(handle1); |
| 797 | } |