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 | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 55 | #define LIBPATH_PREFIX "/nativetest64/" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 56 | #else |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 57 | #define LIBPATH_PREFIX "/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 | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 60 | #define LIBPATH LIBPATH_PREFIX "libdlext_test_fd/libdlext_test_fd.so" |
| 61 | #define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_zip/libdlext_test_zip_zipaligned.zip" |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 62 | #define LIBZIPPATH_WITH_RUNPATH LIBPATH_PREFIX "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 | |
| 172 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) { |
| 176 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 177 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
| 178 | extinfo.library_fd_offset = LIBZIP_OFFSET; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 179 | |
| 180 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 181 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 182 | 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] | 183 | } |
| 184 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 185 | TEST(dlext, android_dlopen_ext_force_load_smoke) { |
| 186 | // 1. Open actual file |
| 187 | void* handle = dlopen("libdlext_test.so", RTLD_NOW); |
| 188 | ASSERT_DL_NOTNULL(handle); |
| 189 | // 2. Open link with force_load flag set |
| 190 | android_dlextinfo extinfo; |
| 191 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 192 | void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo); |
| 193 | ASSERT_DL_NOTNULL(handle2); |
| 194 | ASSERT_TRUE(handle != handle2); |
| 195 | |
| 196 | dlclose(handle2); |
| 197 | dlclose(handle); |
| 198 | } |
| 199 | |
| 200 | TEST(dlext, android_dlopen_ext_force_load_soname_exception) { |
| 201 | // Check if soname lookup still returns already loaded library |
| 202 | // when ANDROID_DLEXT_FORCE_LOAD flag is specified. |
| 203 | void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW); |
| 204 | ASSERT_DL_NOTNULL(handle); |
| 205 | |
| 206 | android_dlextinfo extinfo; |
| 207 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 208 | |
| 209 | // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so |
| 210 | void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); |
| 211 | |
| 212 | ASSERT_DL_NOTNULL(handle2); |
| 213 | ASSERT_TRUE(handle == handle2); |
| 214 | |
| 215 | dlclose(handle2); |
| 216 | dlclose(handle); |
| 217 | } |
| 218 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 219 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
| 220 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
| 221 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 222 | 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] | 223 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 224 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 225 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 226 | ASSERT_DL_NOTNULL(taxicab_number); |
| 227 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 228 | |
| 229 | dlclose(handle); |
| 230 | } |
| 231 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 232 | TEST(dlfcn, dlopen_from_zip_with_dt_runpath) { |
| 233 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH; |
| 234 | |
| 235 | void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW); |
| 236 | |
| 237 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 238 | |
| 239 | typedef void *(* dlopen_b_fn)(); |
| 240 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 241 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 242 | |
| 243 | void *p = fn(); |
| 244 | ASSERT_TRUE(p != nullptr) << dlerror(); |
| 245 | |
| 246 | dlclose(p); |
| 247 | dlclose(handle); |
| 248 | } |
| 249 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 250 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 251 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir"; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 252 | |
| 253 | typedef void (*fn_t)(const char*); |
| 254 | fn_t android_update_LD_LIBRARY_PATH = |
| 255 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 256 | |
| 257 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 258 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 259 | void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 260 | ASSERT_TRUE(handle == nullptr); |
| 261 | |
| 262 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 263 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 264 | 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) << dlerror(); |
| 266 | |
| 267 | int (*fn)(void); |
| 268 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 269 | ASSERT_TRUE(fn != nullptr); |
| 270 | EXPECT_EQ(4, fn()); |
| 271 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 272 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 273 | ASSERT_DL_NOTNULL(taxicab_number); |
| 274 | EXPECT_EQ(1729U, *taxicab_number); |
| 275 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 276 | dlclose(handle); |
| 277 | } |
| 278 | |
| 279 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 280 | TEST_F(DlExtTest, Reserved) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 281 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 282 | -1, 0); |
| 283 | ASSERT_TRUE(start != MAP_FAILED); |
| 284 | android_dlextinfo extinfo; |
| 285 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 286 | extinfo.reserved_addr = start; |
| 287 | extinfo.reserved_size = LIBSIZE; |
| 288 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 289 | ASSERT_DL_NOTNULL(handle_); |
| 290 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 291 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 292 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 293 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 294 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 295 | EXPECT_EQ(4, f()); |
| 296 | } |
| 297 | |
| 298 | TEST_F(DlExtTest, ReservedTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 299 | 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] | 300 | -1, 0); |
| 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 = PAGE_SIZE; |
| 306 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 307 | EXPECT_EQ(nullptr, handle_); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | TEST_F(DlExtTest, ReservedHint) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 311 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 312 | -1, 0); |
| 313 | ASSERT_TRUE(start != MAP_FAILED); |
| 314 | android_dlextinfo extinfo; |
| 315 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 316 | extinfo.reserved_addr = start; |
| 317 | extinfo.reserved_size = LIBSIZE; |
| 318 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 319 | ASSERT_DL_NOTNULL(handle_); |
| 320 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 321 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 322 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 323 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 324 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 325 | EXPECT_EQ(4, f()); |
| 326 | } |
| 327 | |
| 328 | TEST_F(DlExtTest, ReservedHintTooSmall) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 329 | 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] | 330 | -1, 0); |
| 331 | ASSERT_TRUE(start != MAP_FAILED); |
| 332 | android_dlextinfo extinfo; |
| 333 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 334 | extinfo.reserved_addr = start; |
| 335 | extinfo.reserved_size = PAGE_SIZE; |
| 336 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 337 | ASSERT_DL_NOTNULL(handle_); |
| 338 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 339 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 340 | EXPECT_TRUE(reinterpret_cast<void*>(f) < start || |
| 341 | (reinterpret_cast<void*>(f) >= |
| 342 | reinterpret_cast<char*>(start) + PAGE_SIZE)); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 343 | EXPECT_EQ(4, f()); |
| 344 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 345 | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame^] | 346 | TEST_F(DlExtTest, LoadAtFixedAddress) { |
| 347 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 348 | -1, 0); |
| 349 | ASSERT_TRUE(start != MAP_FAILED); |
| 350 | munmap(start, LIBSIZE); |
| 351 | |
| 352 | android_dlextinfo extinfo; |
| 353 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 354 | extinfo.reserved_addr = start; |
| 355 | |
| 356 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 357 | ASSERT_DL_NOTNULL(handle_); |
| 358 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 359 | ASSERT_DL_NOTNULL(f); |
| 360 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
| 361 | EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE); |
| 362 | |
| 363 | EXPECT_EQ(4, f()); |
| 364 | } |
| 365 | |
| 366 | TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) { |
| 367 | void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE, |
| 368 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 369 | ASSERT_TRUE(start != MAP_FAILED); |
| 370 | munmap(start, LIBSIZE + PAGE_SIZE); |
| 371 | void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE, |
| 372 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 373 | ASSERT_TRUE(new_addr != MAP_FAILED); |
| 374 | |
| 375 | android_dlextinfo extinfo; |
| 376 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 377 | extinfo.reserved_addr = start; |
| 378 | |
| 379 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 380 | ASSERT_TRUE(handle_ == nullptr); |
| 381 | } |
| 382 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 383 | class DlExtRelroSharingTest : public DlExtTest { |
| 384 | protected: |
| 385 | virtual void SetUp() { |
| 386 | DlExtTest::SetUp(); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 387 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 388 | -1, 0); |
| 389 | ASSERT_TRUE(start != MAP_FAILED); |
| 390 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 391 | extinfo_.reserved_addr = start; |
| 392 | extinfo_.reserved_size = LIBSIZE; |
| 393 | extinfo_.relro_fd = -1; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 396 | virtual void TearDown() { |
| 397 | DlExtTest::TearDown(); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 398 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 399 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 400 | void CreateRelroFile(const char* lib, const char* relro_file) { |
| 401 | int relro_fd = open(relro_file, O_RDWR | O_TRUNC); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 402 | ASSERT_NOERROR(relro_fd); |
| 403 | |
| 404 | pid_t pid = fork(); |
| 405 | if (pid == 0) { |
| 406 | // child process |
| 407 | extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO; |
| 408 | extinfo_.relro_fd = relro_fd; |
| 409 | void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 410 | if (handle == nullptr) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 411 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 412 | exit(1); |
| 413 | } |
| 414 | exit(0); |
| 415 | } |
| 416 | |
| 417 | // continuing in parent |
| 418 | ASSERT_NOERROR(close(relro_fd)); |
| 419 | ASSERT_NOERROR(pid); |
| 420 | int status; |
| 421 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 422 | ASSERT_TRUE(WIFEXITED(status)); |
| 423 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 424 | |
| 425 | // reopen file for reading so it can be used |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 426 | relro_fd = open(relro_file, O_RDONLY); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 427 | ASSERT_NOERROR(relro_fd); |
| 428 | extinfo_.flags |= ANDROID_DLEXT_USE_RELRO; |
| 429 | extinfo_.relro_fd = relro_fd; |
| 430 | } |
| 431 | |
| 432 | void TryUsingRelro(const char* lib) { |
| 433 | handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 434 | ASSERT_DL_NOTNULL(handle_); |
| 435 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 436 | ASSERT_DL_NOTNULL(f); |
| 437 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 438 | |
| 439 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 440 | ASSERT_DL_NOTNULL(taxicab_number); |
| 441 | EXPECT_EQ(1729U, *taxicab_number); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 444 | void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out); |
| 445 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 446 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 450 | TemporaryFile tf; // Use tf to get an unique filename. |
| 451 | ASSERT_NOERROR(close(tf.fd)); |
| 452 | |
| 453 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 454 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 455 | |
| 456 | // Use destructor of tf to close and unlink the file. |
| 457 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 461 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 462 | ASSERT_NOERROR(close(tf.fd)); |
| 463 | |
| 464 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 465 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 466 | |
| 467 | // Use destructor of tf to close and unlink the file. |
| 468 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 472 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 473 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 474 | |
| 475 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 476 | if (geteuid() != 0) { |
| 477 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 478 | return; |
| 479 | } |
| 480 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 481 | TemporaryFile tf; // Use tf to get an unique filename. |
| 482 | ASSERT_NOERROR(close(tf.fd)); |
| 483 | |
| 484 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
| 485 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 486 | int pipefd[2]; |
| 487 | ASSERT_NOERROR(pipe(pipefd)); |
| 488 | |
| 489 | size_t without_sharing, with_sharing; |
| 490 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing)); |
| 491 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing)); |
| 492 | |
| 493 | // We expect the sharing to save at least 10% of the total PSS. In practice |
| 494 | // it saves 40%+ for this test. |
| 495 | size_t expected_size = without_sharing - (without_sharing/10); |
| 496 | EXPECT_LT(with_sharing, expected_size); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 497 | |
| 498 | // Use destructor of tf to close and unlink the file. |
| 499 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void getPss(pid_t pid, size_t* pss_out) { |
| 503 | pm_kernel_t* kernel; |
| 504 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 505 | |
| 506 | pm_process_t* process; |
| 507 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 508 | |
| 509 | pm_map_t** maps; |
| 510 | size_t num_maps; |
| 511 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 512 | |
| 513 | size_t total_pss = 0; |
| 514 | for (size_t i = 0; i < num_maps; i++) { |
| 515 | pm_memusage_t usage; |
| 516 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 517 | total_pss += usage.pss; |
| 518 | } |
| 519 | *pss_out = total_pss; |
| 520 | |
| 521 | free(maps); |
| 522 | pm_process_destroy(process); |
| 523 | pm_kernel_destroy(kernel); |
| 524 | } |
| 525 | |
| 526 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, |
| 527 | size_t* pss_out) { |
| 528 | const int CHILDREN = 20; |
| 529 | |
| 530 | // Create children |
| 531 | pid_t childpid[CHILDREN]; |
| 532 | int childpipe[CHILDREN]; |
| 533 | for (int i=0; i<CHILDREN; ++i) { |
| 534 | char read_buf; |
| 535 | int child_done_pipe[2], parent_done_pipe[2]; |
| 536 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 537 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 538 | |
| 539 | pid_t child = fork(); |
| 540 | if (child == 0) { |
| 541 | // close the 'wrong' ends of the pipes in the child |
| 542 | close(child_done_pipe[0]); |
| 543 | close(parent_done_pipe[1]); |
| 544 | |
| 545 | // open the library |
| 546 | void* handle; |
| 547 | if (share_relro) { |
| 548 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 549 | } else { |
| 550 | handle = dlopen(lib, RTLD_NOW); |
| 551 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 552 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 553 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 554 | exit(1); |
| 555 | } |
| 556 | |
| 557 | // close write end of child_done_pipe to signal the parent that we're done. |
| 558 | close(child_done_pipe[1]); |
| 559 | |
| 560 | // wait for the parent to close parent_done_pipe, then exit |
| 561 | read(parent_done_pipe[0], &read_buf, 1); |
| 562 | exit(0); |
| 563 | } |
| 564 | |
| 565 | ASSERT_NOERROR(child); |
| 566 | |
| 567 | // close the 'wrong' ends of the pipes in the parent |
| 568 | close(child_done_pipe[1]); |
| 569 | close(parent_done_pipe[0]); |
| 570 | |
| 571 | // wait for the child to be done |
| 572 | read(child_done_pipe[0], &read_buf, 1); |
| 573 | close(child_done_pipe[0]); |
| 574 | |
| 575 | // save the child's pid and the parent_done_pipe |
| 576 | childpid[i] = child; |
| 577 | childpipe[i] = parent_done_pipe[1]; |
| 578 | } |
| 579 | |
| 580 | // Sum the PSS of all the children |
| 581 | size_t total_pss = 0; |
| 582 | for (int i=0; i<CHILDREN; ++i) { |
| 583 | size_t child_pss; |
| 584 | ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss)); |
| 585 | total_pss += child_pss; |
| 586 | } |
| 587 | *pss_out = total_pss; |
| 588 | |
| 589 | // Close pipes and wait for children to exit |
| 590 | for (int i=0; i<CHILDREN; ++i) { |
| 591 | ASSERT_NOERROR(close(childpipe[i])); |
| 592 | } |
| 593 | for (int i=0; i<CHILDREN; ++i) { |
| 594 | int status; |
| 595 | ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0)); |
| 596 | ASSERT_TRUE(WIFEXITED(status)); |
| 597 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 598 | } |
| 599 | } |