blob: 5b3136458d9363b55c628138ed0e233425a6b53b [file] [log] [blame]
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001/*
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 Cui16f7f8d2014-11-04 11:08:05 -080020#include <elf.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000021#include <errno.h>
22#include <fcntl.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080023#include <inttypes.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000024#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000027#include <android/dlext.h>
28#include <sys/mman.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010029#include <sys/types.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000030#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000031
Torne (Richard Coles)26052612014-05-02 14:57:42 +010032#include <pagemap/pagemap.h>
33
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000034
35#define ASSERT_DL_NOTNULL(ptr) \
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070036 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000037
38#define ASSERT_DL_ZERO(i) \
39 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
40
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000041#define ASSERT_NOERROR(i) \
42 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
43
Yabin Cui16f7f8d2014-11-04 11:08:05 -080044#define ASSERT_SUBSTR(needle, haystack) \
45 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
46
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000047
48typedef int (*fn)(void);
49#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010050#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000051#define LIBSIZE 1024*1024 // how much address space to reserve for it
52
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070053#if defined(__LP64__)
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070054#define LIBPATH_PREFIX "%s/nativetest64/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070055#else
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070056#define LIBPATH_PREFIX "%s/nativetest/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070057#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000058
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070059#define LIBPATH LIBPATH_PREFIX "libdlext_test_fd.so"
Ying Wang667853d2014-10-08 16:22:03 -070060#define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070061
62#define LIBZIP_OFFSET 2*PAGE_SIZE
63
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000064class DlExtTest : public ::testing::Test {
65protected:
66 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070067 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000068 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070069 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
70 ASSERT_TRUE(h == nullptr);
71 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
72 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000073 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070074 ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000075 }
76
77 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070078 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000079 ASSERT_DL_ZERO(dlclose(handle_));
80 }
81 }
82
83 void* handle_;
84};
85
86TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070087 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000088 ASSERT_DL_NOTNULL(handle_);
89 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
90 ASSERT_DL_NOTNULL(f);
91 EXPECT_EQ(4, f());
92}
93
94TEST_F(DlExtTest, ExtInfoNoFlags) {
95 android_dlextinfo extinfo;
96 extinfo.flags = 0;
97 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
98 ASSERT_DL_NOTNULL(handle_);
99 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
100 ASSERT_DL_NOTNULL(f);
101 EXPECT_EQ(4, f());
102}
103
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700104TEST_F(DlExtTest, ExtInfoUseFd) {
105 const char* android_data = getenv("ANDROID_DATA");
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700106 ASSERT_TRUE(android_data != nullptr);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700107 char lib_path[PATH_MAX];
108 snprintf(lib_path, sizeof(lib_path), LIBPATH, android_data);
109
110 android_dlextinfo extinfo;
111 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
112 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
113 ASSERT_TRUE(extinfo.library_fd != -1);
114 handle_ = android_dlopen_ext(lib_path, RTLD_NOW, &extinfo);
115 ASSERT_DL_NOTNULL(handle_);
116 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
117 ASSERT_DL_NOTNULL(f);
118 EXPECT_EQ(4, f());
119}
120
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700121TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
122 const char* android_data = getenv("ANDROID_DATA");
123 ASSERT_TRUE(android_data != nullptr);
124
125 char lib_path[PATH_MAX];
126 snprintf(lib_path, sizeof(lib_path), LIBZIPPATH, android_data);
127
128 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700129 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700130 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700131 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700132
133 handle_ = android_dlopen_ext(lib_path, RTLD_NOW, &extinfo);
134 ASSERT_DL_NOTNULL(handle_);
135
136 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
137 ASSERT_DL_NOTNULL(f);
138 EXPECT_EQ(4, f());
139}
140
141TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
142 const char* android_data = getenv("ANDROID_DATA");
143 ASSERT_TRUE(android_data != nullptr);
144
145 char lib_path[PATH_MAX];
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800146 snprintf(lib_path, sizeof(lib_path), LIBPATH, android_data);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700147
148 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700149 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700150 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700151 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700152
153 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
154 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700155 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
156
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800157 // Test an address above 2^44, for http://b/18178121 .
158 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700160 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800161 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
162
163 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
164 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
165 ASSERT_TRUE(handle_ == nullptr);
166 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
167
168 extinfo.library_fd_offset = PAGE_SIZE;
169 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
170 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171 ASSERT_STREQ("dlopen failed: \"libname_placeholder\" has bad ELF magic", dlerror());
172
173 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700174}
175
176TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
177 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700178 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
179 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700180
181 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
182 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700183 ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror());
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700184}
185
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000186TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700187 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000188 -1, 0);
189 ASSERT_TRUE(start != MAP_FAILED);
190 android_dlextinfo extinfo;
191 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
192 extinfo.reserved_addr = start;
193 extinfo.reserved_size = LIBSIZE;
194 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
195 ASSERT_DL_NOTNULL(handle_);
196 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
197 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700198 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000199 EXPECT_LT(reinterpret_cast<void*>(f),
200 reinterpret_cast<char*>(start) + LIBSIZE);
201 EXPECT_EQ(4, f());
202}
203
204TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700205 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000206 -1, 0);
207 ASSERT_TRUE(start != MAP_FAILED);
208 android_dlextinfo extinfo;
209 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
210 extinfo.reserved_addr = start;
211 extinfo.reserved_size = PAGE_SIZE;
212 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700213 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000214}
215
216TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700217 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000218 -1, 0);
219 ASSERT_TRUE(start != MAP_FAILED);
220 android_dlextinfo extinfo;
221 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
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 Hsieha2c6ae62014-08-27 13:45:37 -0700228 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000229 EXPECT_LT(reinterpret_cast<void*>(f),
230 reinterpret_cast<char*>(start) + LIBSIZE);
231 EXPECT_EQ(4, f());
232}
233
234TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700235 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000236 -1, 0);
237 ASSERT_TRUE(start != MAP_FAILED);
238 android_dlextinfo extinfo;
239 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
240 extinfo.reserved_addr = start;
241 extinfo.reserved_size = PAGE_SIZE;
242 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
243 ASSERT_DL_NOTNULL(handle_);
244 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
245 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700246 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
247 (reinterpret_cast<void*>(f) >=
248 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000249 EXPECT_EQ(4, f());
250}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000251
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100252class DlExtRelroSharingTest : public DlExtTest {
253protected:
254 virtual void SetUp() {
255 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700256 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100257 -1, 0);
258 ASSERT_TRUE(start != MAP_FAILED);
259 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
260 extinfo_.reserved_addr = start;
261 extinfo_.reserved_size = LIBSIZE;
262 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000263
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100264 const char* android_data = getenv("ANDROID_DATA");
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700265 ASSERT_TRUE(android_data != nullptr);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100266 snprintf(relro_file_, sizeof(relro_file_), "%s/local/tmp/libdlext_test.relro", android_data);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000267 }
268
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100269 virtual void TearDown() {
270 DlExtTest::TearDown();
271 if (extinfo_.relro_fd != -1) {
272 ASSERT_NOERROR(close(extinfo_.relro_fd));
273 }
274 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000275
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100276 void CreateRelroFile(const char* lib) {
277 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
278 ASSERT_NOERROR(relro_fd);
279
280 pid_t pid = fork();
281 if (pid == 0) {
282 // child process
283 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
284 extinfo_.relro_fd = relro_fd;
285 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700286 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100287 fprintf(stderr, "in child: %s\n", dlerror());
288 exit(1);
289 }
290 exit(0);
291 }
292
293 // continuing in parent
294 ASSERT_NOERROR(close(relro_fd));
295 ASSERT_NOERROR(pid);
296 int status;
297 ASSERT_EQ(pid, waitpid(pid, &status, 0));
298 ASSERT_TRUE(WIFEXITED(status));
299 ASSERT_EQ(0, WEXITSTATUS(status));
300
301 // reopen file for reading so it can be used
302 relro_fd = open(relro_file_, O_RDONLY);
303 ASSERT_NOERROR(relro_fd);
304 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
305 extinfo_.relro_fd = relro_fd;
306 }
307
308 void TryUsingRelro(const char* lib) {
309 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
310 ASSERT_DL_NOTNULL(handle_);
311 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
312 ASSERT_DL_NOTNULL(f);
313 EXPECT_EQ(4, f());
314 }
315
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100316 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
317
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100318 android_dlextinfo extinfo_;
319 char relro_file_[PATH_MAX];
320};
321
322TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
323 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
324 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
325}
326
327TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
328 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO));
329 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
330}
331
332TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
333 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000334 ASSERT_NOERROR(relro_fd);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000335 ASSERT_NOERROR(close(relro_fd));
336
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100337 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000338}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100339
340TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700341 if (geteuid() != 0) {
342 GTEST_LOG_(INFO) << "This test must be run as root.\n";
343 return;
344 }
345
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100346 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
347 int relro_fd = open(relro_file_, O_RDONLY);
348 ASSERT_NOERROR(relro_fd);
349 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
350 extinfo_.relro_fd = relro_fd;
351 int pipefd[2];
352 ASSERT_NOERROR(pipe(pipefd));
353
354 size_t without_sharing, with_sharing;
355 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
356 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
357
358 // We expect the sharing to save at least 10% of the total PSS. In practice
359 // it saves 40%+ for this test.
360 size_t expected_size = without_sharing - (without_sharing/10);
361 EXPECT_LT(with_sharing, expected_size);
362}
363
364void getPss(pid_t pid, size_t* pss_out) {
365 pm_kernel_t* kernel;
366 ASSERT_EQ(0, pm_kernel_create(&kernel));
367
368 pm_process_t* process;
369 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
370
371 pm_map_t** maps;
372 size_t num_maps;
373 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
374
375 size_t total_pss = 0;
376 for (size_t i = 0; i < num_maps; i++) {
377 pm_memusage_t usage;
378 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
379 total_pss += usage.pss;
380 }
381 *pss_out = total_pss;
382
383 free(maps);
384 pm_process_destroy(process);
385 pm_kernel_destroy(kernel);
386}
387
388void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
389 size_t* pss_out) {
390 const int CHILDREN = 20;
391
392 // Create children
393 pid_t childpid[CHILDREN];
394 int childpipe[CHILDREN];
395 for (int i=0; i<CHILDREN; ++i) {
396 char read_buf;
397 int child_done_pipe[2], parent_done_pipe[2];
398 ASSERT_NOERROR(pipe(child_done_pipe));
399 ASSERT_NOERROR(pipe(parent_done_pipe));
400
401 pid_t child = fork();
402 if (child == 0) {
403 // close the 'wrong' ends of the pipes in the child
404 close(child_done_pipe[0]);
405 close(parent_done_pipe[1]);
406
407 // open the library
408 void* handle;
409 if (share_relro) {
410 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
411 } else {
412 handle = dlopen(lib, RTLD_NOW);
413 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700414 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100415 fprintf(stderr, "in child: %s\n", dlerror());
416 exit(1);
417 }
418
419 // close write end of child_done_pipe to signal the parent that we're done.
420 close(child_done_pipe[1]);
421
422 // wait for the parent to close parent_done_pipe, then exit
423 read(parent_done_pipe[0], &read_buf, 1);
424 exit(0);
425 }
426
427 ASSERT_NOERROR(child);
428
429 // close the 'wrong' ends of the pipes in the parent
430 close(child_done_pipe[1]);
431 close(parent_done_pipe[0]);
432
433 // wait for the child to be done
434 read(child_done_pipe[0], &read_buf, 1);
435 close(child_done_pipe[0]);
436
437 // save the child's pid and the parent_done_pipe
438 childpid[i] = child;
439 childpipe[i] = parent_done_pipe[1];
440 }
441
442 // Sum the PSS of all the children
443 size_t total_pss = 0;
444 for (int i=0; i<CHILDREN; ++i) {
445 size_t child_pss;
446 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
447 total_pss += child_pss;
448 }
449 *pss_out = total_pss;
450
451 // Close pipes and wait for children to exit
452 for (int i=0; i<CHILDREN; ++i) {
453 ASSERT_NOERROR(close(childpipe[i]));
454 }
455 for (int i=0; i<CHILDREN; ++i) {
456 int status;
457 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
458 ASSERT_TRUE(WIFEXITED(status));
459 ASSERT_EQ(0, WEXITSTATUS(status));
460 }
461}