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 | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 55 | #define LIBPATH_PREFIX "/nativetest64/libdlext_test_fd/" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 56 | #else |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 57 | #define LIBPATH_PREFIX "/nativetest/libdlext_test_fd/" |
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 | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 60 | #define LIBPATH LIBPATH_PREFIX "libdlext_test_fd.so" |
Ying Wang | 667853d | 2014-10-08 16:22:03 -0700 | [diff] [blame] | 61 | #define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip" |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 62 | |
| 63 | #define LIBZIP_OFFSET 2*PAGE_SIZE |
| 64 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 65 | class DlExtTest : public ::testing::Test { |
| 66 | protected: |
| 67 | virtual void SetUp() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 68 | handle_ = nullptr; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 69 | // verify that we don't have the library loaded already |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 70 | void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD); |
| 71 | ASSERT_TRUE(h == nullptr); |
| 72 | h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD); |
| 73 | ASSERT_TRUE(h == nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 74 | // call dlerror() to swallow the error, and check it was the one we wanted |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 75 | 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] | 76 | } |
| 77 | |
| 78 | virtual void TearDown() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 79 | if (handle_ != nullptr) { |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 80 | ASSERT_DL_ZERO(dlclose(handle_)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void* handle_; |
| 85 | }; |
| 86 | |
| 87 | TEST_F(DlExtTest, ExtInfoNull) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 88 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 89 | ASSERT_DL_NOTNULL(handle_); |
| 90 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 91 | ASSERT_DL_NOTNULL(f); |
| 92 | EXPECT_EQ(4, f()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(DlExtTest, ExtInfoNoFlags) { |
| 96 | android_dlextinfo extinfo; |
| 97 | extinfo.flags = 0; |
| 98 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 99 | ASSERT_DL_NOTNULL(handle_); |
| 100 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 101 | ASSERT_DL_NOTNULL(f); |
| 102 | EXPECT_EQ(4, f()); |
| 103 | } |
| 104 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 105 | TEST_F(DlExtTest, ExtInfoUseFd) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 106 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH; |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 107 | |
| 108 | android_dlextinfo extinfo; |
| 109 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 110 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(extinfo.library_fd != -1); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 112 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 113 | ASSERT_DL_NOTNULL(handle_); |
| 114 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 115 | ASSERT_DL_NOTNULL(f); |
| 116 | EXPECT_EQ(4, f()); |
| 117 | } |
| 118 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 119 | TEST_F(DlExtTest, ExtInfoUseFdWithOffset) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 120 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 121 | |
| 122 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 123 | 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^] | 124 | 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] | 125 | extinfo.library_fd_offset = LIBZIP_OFFSET; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 126 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 127 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 128 | ASSERT_DL_NOTNULL(handle_); |
| 129 | |
| 130 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 131 | ASSERT_DL_NOTNULL(f); |
| 132 | EXPECT_EQ(4, f()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 136 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 137 | |
| 138 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 139 | 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^] | 140 | 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] | 141 | extinfo.library_fd_offset = 17; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 142 | |
| 143 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 144 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 145 | ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror()); |
| 146 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 147 | // Test an address above 2^44, for http://b/18178121 . |
| 148 | extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 149 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 150 | ASSERT_TRUE(handle_ == nullptr); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 151 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror()); |
| 152 | |
| 153 | extinfo.library_fd_offset = 0LL - PAGE_SIZE; |
| 154 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 155 | ASSERT_TRUE(handle_ == nullptr); |
| 156 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror()); |
| 157 | |
| 158 | extinfo.library_fd_offset = PAGE_SIZE; |
| 159 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 160 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 161 | ASSERT_STREQ("dlopen failed: \"libname_placeholder\" has bad ELF magic", dlerror()); |
| 162 | |
| 163 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) { |
| 167 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 168 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
| 169 | extinfo.library_fd_offset = LIBZIP_OFFSET; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 170 | |
| 171 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 172 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 173 | 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] | 174 | } |
| 175 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame^] | 176 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
| 177 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
| 178 | |
| 179 | void* handle = dlopen((lib_path + "!libdir/libdlext_test_fd.so").c_str(), RTLD_NOW); |
| 180 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 181 | |
| 182 | int (*fn)(void); |
| 183 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 184 | ASSERT_TRUE(fn != nullptr); |
| 185 | EXPECT_EQ(4, fn()); |
| 186 | |
| 187 | dlclose(handle); |
| 188 | } |
| 189 | |
| 190 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
| 191 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!libdir"; |
| 192 | |
| 193 | typedef void (*fn_t)(const char*); |
| 194 | fn_t android_update_LD_LIBRARY_PATH = |
| 195 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 196 | |
| 197 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 198 | |
| 199 | void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW); |
| 200 | ASSERT_TRUE(handle == nullptr); |
| 201 | |
| 202 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 203 | |
| 204 | handle = dlopen("libdlext_test_fd.so", RTLD_NOW); |
| 205 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 206 | |
| 207 | int (*fn)(void); |
| 208 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 209 | ASSERT_TRUE(fn != nullptr); |
| 210 | EXPECT_EQ(4, fn()); |
| 211 | |
| 212 | dlclose(handle); |
| 213 | } |
| 214 | |
| 215 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 216 | TEST_F(DlExtTest, Reserved) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 217 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 218 | -1, 0); |
| 219 | ASSERT_TRUE(start != MAP_FAILED); |
| 220 | android_dlextinfo extinfo; |
| 221 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 222 | extinfo.reserved_addr = start; |
| 223 | extinfo.reserved_size = LIBSIZE; |
| 224 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 225 | ASSERT_DL_NOTNULL(handle_); |
| 226 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 227 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 228 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 229 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 230 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 231 | EXPECT_EQ(4, f()); |
| 232 | } |
| 233 | |
| 234 | TEST_F(DlExtTest, ReservedTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 235 | 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] | 236 | -1, 0); |
| 237 | ASSERT_TRUE(start != MAP_FAILED); |
| 238 | android_dlextinfo extinfo; |
| 239 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 240 | extinfo.reserved_addr = start; |
| 241 | extinfo.reserved_size = PAGE_SIZE; |
| 242 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 243 | EXPECT_EQ(nullptr, handle_); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST_F(DlExtTest, ReservedHint) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 247 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 248 | -1, 0); |
| 249 | ASSERT_TRUE(start != MAP_FAILED); |
| 250 | android_dlextinfo extinfo; |
| 251 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 252 | extinfo.reserved_addr = start; |
| 253 | extinfo.reserved_size = LIBSIZE; |
| 254 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 255 | ASSERT_DL_NOTNULL(handle_); |
| 256 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 257 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 258 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 259 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 260 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 261 | EXPECT_EQ(4, f()); |
| 262 | } |
| 263 | |
| 264 | TEST_F(DlExtTest, ReservedHintTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 265 | 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] | 266 | -1, 0); |
| 267 | ASSERT_TRUE(start != MAP_FAILED); |
| 268 | android_dlextinfo extinfo; |
| 269 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 270 | extinfo.reserved_addr = start; |
| 271 | extinfo.reserved_size = PAGE_SIZE; |
| 272 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 273 | ASSERT_DL_NOTNULL(handle_); |
| 274 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 275 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(reinterpret_cast<void*>(f) < start || |
| 277 | (reinterpret_cast<void*>(f) >= |
| 278 | reinterpret_cast<char*>(start) + PAGE_SIZE)); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 279 | EXPECT_EQ(4, f()); |
| 280 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 281 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 282 | class DlExtRelroSharingTest : public DlExtTest { |
| 283 | protected: |
| 284 | virtual void SetUp() { |
| 285 | DlExtTest::SetUp(); |
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) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 287 | -1, 0); |
| 288 | ASSERT_TRUE(start != MAP_FAILED); |
| 289 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 290 | extinfo_.reserved_addr = start; |
| 291 | extinfo_.reserved_size = LIBSIZE; |
| 292 | extinfo_.relro_fd = -1; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 295 | virtual void TearDown() { |
| 296 | DlExtTest::TearDown(); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 297 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 298 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 299 | void CreateRelroFile(const char* lib, const char* relro_file) { |
| 300 | int relro_fd = open(relro_file, O_RDWR | O_TRUNC); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 301 | ASSERT_NOERROR(relro_fd); |
| 302 | |
| 303 | pid_t pid = fork(); |
| 304 | if (pid == 0) { |
| 305 | // child process |
| 306 | extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO; |
| 307 | extinfo_.relro_fd = relro_fd; |
| 308 | void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 309 | if (handle == nullptr) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 310 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 311 | exit(1); |
| 312 | } |
| 313 | exit(0); |
| 314 | } |
| 315 | |
| 316 | // continuing in parent |
| 317 | ASSERT_NOERROR(close(relro_fd)); |
| 318 | ASSERT_NOERROR(pid); |
| 319 | int status; |
| 320 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 321 | ASSERT_TRUE(WIFEXITED(status)); |
| 322 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 323 | |
| 324 | // reopen file for reading so it can be used |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 325 | relro_fd = open(relro_file, O_RDONLY); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 326 | ASSERT_NOERROR(relro_fd); |
| 327 | extinfo_.flags |= ANDROID_DLEXT_USE_RELRO; |
| 328 | extinfo_.relro_fd = relro_fd; |
| 329 | } |
| 330 | |
| 331 | void TryUsingRelro(const char* lib) { |
| 332 | handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 333 | ASSERT_DL_NOTNULL(handle_); |
| 334 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 335 | ASSERT_DL_NOTNULL(f); |
| 336 | EXPECT_EQ(4, f()); |
| 337 | } |
| 338 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 339 | void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out); |
| 340 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 341 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 345 | TemporaryFile tf; // Use tf to get an unique filename. |
| 346 | ASSERT_NOERROR(close(tf.fd)); |
| 347 | |
| 348 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 349 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 350 | |
| 351 | // Use destructor of tf to close and unlink the file. |
| 352 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 356 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 357 | ASSERT_NOERROR(close(tf.fd)); |
| 358 | |
| 359 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 360 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 361 | |
| 362 | // Use destructor of tf to close and unlink the file. |
| 363 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 367 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 368 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 369 | |
| 370 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 371 | if (geteuid() != 0) { |
| 372 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 373 | return; |
| 374 | } |
| 375 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 376 | TemporaryFile tf; // Use tf to get an unique filename. |
| 377 | ASSERT_NOERROR(close(tf.fd)); |
| 378 | |
| 379 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
| 380 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 381 | int pipefd[2]; |
| 382 | ASSERT_NOERROR(pipe(pipefd)); |
| 383 | |
| 384 | size_t without_sharing, with_sharing; |
| 385 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing)); |
| 386 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing)); |
| 387 | |
| 388 | // We expect the sharing to save at least 10% of the total PSS. In practice |
| 389 | // it saves 40%+ for this test. |
| 390 | size_t expected_size = without_sharing - (without_sharing/10); |
| 391 | EXPECT_LT(with_sharing, expected_size); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 392 | |
| 393 | // Use destructor of tf to close and unlink the file. |
| 394 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void getPss(pid_t pid, size_t* pss_out) { |
| 398 | pm_kernel_t* kernel; |
| 399 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 400 | |
| 401 | pm_process_t* process; |
| 402 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 403 | |
| 404 | pm_map_t** maps; |
| 405 | size_t num_maps; |
| 406 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 407 | |
| 408 | size_t total_pss = 0; |
| 409 | for (size_t i = 0; i < num_maps; i++) { |
| 410 | pm_memusage_t usage; |
| 411 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 412 | total_pss += usage.pss; |
| 413 | } |
| 414 | *pss_out = total_pss; |
| 415 | |
| 416 | free(maps); |
| 417 | pm_process_destroy(process); |
| 418 | pm_kernel_destroy(kernel); |
| 419 | } |
| 420 | |
| 421 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, |
| 422 | size_t* pss_out) { |
| 423 | const int CHILDREN = 20; |
| 424 | |
| 425 | // Create children |
| 426 | pid_t childpid[CHILDREN]; |
| 427 | int childpipe[CHILDREN]; |
| 428 | for (int i=0; i<CHILDREN; ++i) { |
| 429 | char read_buf; |
| 430 | int child_done_pipe[2], parent_done_pipe[2]; |
| 431 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 432 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 433 | |
| 434 | pid_t child = fork(); |
| 435 | if (child == 0) { |
| 436 | // close the 'wrong' ends of the pipes in the child |
| 437 | close(child_done_pipe[0]); |
| 438 | close(parent_done_pipe[1]); |
| 439 | |
| 440 | // open the library |
| 441 | void* handle; |
| 442 | if (share_relro) { |
| 443 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 444 | } else { |
| 445 | handle = dlopen(lib, RTLD_NOW); |
| 446 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 447 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 448 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 449 | exit(1); |
| 450 | } |
| 451 | |
| 452 | // close write end of child_done_pipe to signal the parent that we're done. |
| 453 | close(child_done_pipe[1]); |
| 454 | |
| 455 | // wait for the parent to close parent_done_pipe, then exit |
| 456 | read(parent_done_pipe[0], &read_buf, 1); |
| 457 | exit(0); |
| 458 | } |
| 459 | |
| 460 | ASSERT_NOERROR(child); |
| 461 | |
| 462 | // close the 'wrong' ends of the pipes in the parent |
| 463 | close(child_done_pipe[1]); |
| 464 | close(parent_done_pipe[0]); |
| 465 | |
| 466 | // wait for the child to be done |
| 467 | read(child_done_pipe[0], &read_buf, 1); |
| 468 | close(child_done_pipe[0]); |
| 469 | |
| 470 | // save the child's pid and the parent_done_pipe |
| 471 | childpid[i] = child; |
| 472 | childpipe[i] = parent_done_pipe[1]; |
| 473 | } |
| 474 | |
| 475 | // Sum the PSS of all the children |
| 476 | size_t total_pss = 0; |
| 477 | for (int i=0; i<CHILDREN; ++i) { |
| 478 | size_t child_pss; |
| 479 | ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss)); |
| 480 | total_pss += child_pss; |
| 481 | } |
| 482 | *pss_out = total_pss; |
| 483 | |
| 484 | // Close pipes and wait for children to exit |
| 485 | for (int i=0; i<CHILDREN; ++i) { |
| 486 | ASSERT_NOERROR(close(childpipe[i])); |
| 487 | } |
| 488 | for (int i=0; i<CHILDREN; ++i) { |
| 489 | int status; |
| 490 | ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0)); |
| 491 | ASSERT_TRUE(WIFEXITED(status)); |
| 492 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 493 | } |
| 494 | } |