blob: 700abff5ec9190dc4bb0fb7c672c367a4d3135e6 [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
Yabin Cui294d1e22014-12-07 20:43:37 -080034#include "TemporaryFile.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000035
36#define ASSERT_DL_NOTNULL(ptr) \
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070037 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000038
39#define ASSERT_DL_ZERO(i) \
40 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
41
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000042#define ASSERT_NOERROR(i) \
43 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
44
Yabin Cui16f7f8d2014-11-04 11:08:05 -080045#define ASSERT_SUBSTR(needle, haystack) \
46 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
47
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000048
49typedef int (*fn)(void);
50#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010051#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000052#define LIBSIZE 1024*1024 // how much address space to reserve for it
53
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070054#if defined(__LP64__)
Dmitriy Ivanov52393a52015-03-18 22:50:01 -070055#define LIBPATH_PREFIX "/nativetest64/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070056#else
Dmitriy Ivanov52393a52015-03-18 22:50:01 -070057#define LIBPATH_PREFIX "/nativetest/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070058#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000059
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070060#define LIBPATH LIBPATH_PREFIX "libdlext_test_fd.so"
Ying Wang667853d2014-10-08 16:22:03 -070061#define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070062
63#define LIBZIP_OFFSET 2*PAGE_SIZE
64
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000065class DlExtTest : public ::testing::Test {
66protected:
67 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070068 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000069 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070070 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)12bbb912014-02-06 14:34:21 +000074 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070075 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 +000076 }
77
78 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070079 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000080 ASSERT_DL_ZERO(dlclose(handle_));
81 }
82 }
83
84 void* handle_;
85};
86
87TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070088 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000089 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
95TEST_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 Ivanov04dc91a2014-07-01 14:10:16 -0700105TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700106 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700107
108 android_dlextinfo extinfo;
109 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700110 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700111 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700112 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700113 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 Ivanov07e5bc12014-10-03 17:52:44 -0700119TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700120 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700121
122 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700123 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700124 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700125 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700126
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700127 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700128 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
135TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700136 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700137
138 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700139 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700140 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700141 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700142
143 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
144 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700145 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
146
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800147 // Test an address above 2^44, for http://b/18178121 .
148 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700149 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700150 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800151 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 Ivanova6c12792014-10-21 12:09:18 -0700161 ASSERT_STREQ("dlopen failed: \"libname_placeholder\" has bad ELF magic", dlerror());
162
163 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700164}
165
166TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
167 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700168 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
169 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700170
171 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
172 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700173 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 -0700174}
175
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700176TEST(dlext, android_dlopen_ext_force_load_smoke) {
177 // 1. Open actual file
178 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
179 ASSERT_DL_NOTNULL(handle);
180 // 2. Open link with force_load flag set
181 android_dlextinfo extinfo;
182 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
183 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
184 ASSERT_DL_NOTNULL(handle2);
185 ASSERT_TRUE(handle != handle2);
186
187 dlclose(handle2);
188 dlclose(handle);
189}
190
191TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
192 // Check if soname lookup still returns already loaded library
193 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
194 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
195 ASSERT_DL_NOTNULL(handle);
196
197 android_dlextinfo extinfo;
198 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
199
200 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
201 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
202
203 ASSERT_DL_NOTNULL(handle2);
204 ASSERT_TRUE(handle == handle2);
205
206 dlclose(handle2);
207 dlclose(handle);
208}
209
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700210TEST(dlfcn, dlopen_from_zip_absolute_path) {
211 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
212
213 void* handle = dlopen((lib_path + "!libdir/libdlext_test_fd.so").c_str(), RTLD_NOW);
214 ASSERT_TRUE(handle != nullptr) << dlerror();
215
216 int (*fn)(void);
217 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
218 ASSERT_TRUE(fn != nullptr);
219 EXPECT_EQ(4, fn());
220
221 dlclose(handle);
222}
223
224TEST(dlfcn, dlopen_from_zip_ld_library_path) {
225 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!libdir";
226
227 typedef void (*fn_t)(const char*);
228 fn_t android_update_LD_LIBRARY_PATH =
229 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
230
231 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
232
233 void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
234 ASSERT_TRUE(handle == nullptr);
235
236 android_update_LD_LIBRARY_PATH(lib_path.c_str());
237
238 handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
239 ASSERT_TRUE(handle != nullptr) << dlerror();
240
241 int (*fn)(void);
242 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
243 ASSERT_TRUE(fn != nullptr);
244 EXPECT_EQ(4, fn());
245
246 dlclose(handle);
247}
248
249
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000250TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700251 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000252 -1, 0);
253 ASSERT_TRUE(start != MAP_FAILED);
254 android_dlextinfo extinfo;
255 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
256 extinfo.reserved_addr = start;
257 extinfo.reserved_size = LIBSIZE;
258 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
259 ASSERT_DL_NOTNULL(handle_);
260 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
261 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700262 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000263 EXPECT_LT(reinterpret_cast<void*>(f),
264 reinterpret_cast<char*>(start) + LIBSIZE);
265 EXPECT_EQ(4, f());
266}
267
268TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700269 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000270 -1, 0);
271 ASSERT_TRUE(start != MAP_FAILED);
272 android_dlextinfo extinfo;
273 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
274 extinfo.reserved_addr = start;
275 extinfo.reserved_size = PAGE_SIZE;
276 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700277 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000278}
279
280TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700281 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000282 -1, 0);
283 ASSERT_TRUE(start != MAP_FAILED);
284 android_dlextinfo extinfo;
285 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
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 Hsieha2c6ae62014-08-27 13:45:37 -0700292 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000293 EXPECT_LT(reinterpret_cast<void*>(f),
294 reinterpret_cast<char*>(start) + LIBSIZE);
295 EXPECT_EQ(4, f());
296}
297
298TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700299 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000300 -1, 0);
301 ASSERT_TRUE(start != MAP_FAILED);
302 android_dlextinfo extinfo;
303 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
304 extinfo.reserved_addr = start;
305 extinfo.reserved_size = PAGE_SIZE;
306 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
307 ASSERT_DL_NOTNULL(handle_);
308 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
309 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700310 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
311 (reinterpret_cast<void*>(f) >=
312 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000313 EXPECT_EQ(4, f());
314}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000315
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100316class DlExtRelroSharingTest : public DlExtTest {
317protected:
318 virtual void SetUp() {
319 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700320 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100321 -1, 0);
322 ASSERT_TRUE(start != MAP_FAILED);
323 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
324 extinfo_.reserved_addr = start;
325 extinfo_.reserved_size = LIBSIZE;
326 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000327 }
328
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100329 virtual void TearDown() {
330 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100331 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000332
Yabin Cui294d1e22014-12-07 20:43:37 -0800333 void CreateRelroFile(const char* lib, const char* relro_file) {
334 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100335 ASSERT_NOERROR(relro_fd);
336
337 pid_t pid = fork();
338 if (pid == 0) {
339 // child process
340 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
341 extinfo_.relro_fd = relro_fd;
342 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700343 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100344 fprintf(stderr, "in child: %s\n", dlerror());
345 exit(1);
346 }
347 exit(0);
348 }
349
350 // continuing in parent
351 ASSERT_NOERROR(close(relro_fd));
352 ASSERT_NOERROR(pid);
353 int status;
354 ASSERT_EQ(pid, waitpid(pid, &status, 0));
355 ASSERT_TRUE(WIFEXITED(status));
356 ASSERT_EQ(0, WEXITSTATUS(status));
357
358 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800359 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100360 ASSERT_NOERROR(relro_fd);
361 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
362 extinfo_.relro_fd = relro_fd;
363 }
364
365 void TryUsingRelro(const char* lib) {
366 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
367 ASSERT_DL_NOTNULL(handle_);
368 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
369 ASSERT_DL_NOTNULL(f);
370 EXPECT_EQ(4, f());
371 }
372
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100373 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
374
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100375 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100376};
377
378TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800379 TemporaryFile tf; // Use tf to get an unique filename.
380 ASSERT_NOERROR(close(tf.fd));
381
382 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100383 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800384
385 // Use destructor of tf to close and unlink the file.
386 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100387}
388
389TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800390 TemporaryFile tf; // // Use tf to get an unique filename.
391 ASSERT_NOERROR(close(tf.fd));
392
393 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100394 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800395
396 // Use destructor of tf to close and unlink the file.
397 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100398}
399
400TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100401 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000402}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100403
404TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700405 if (geteuid() != 0) {
406 GTEST_LOG_(INFO) << "This test must be run as root.\n";
407 return;
408 }
409
Yabin Cui294d1e22014-12-07 20:43:37 -0800410 TemporaryFile tf; // Use tf to get an unique filename.
411 ASSERT_NOERROR(close(tf.fd));
412
413 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
414
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100415 int pipefd[2];
416 ASSERT_NOERROR(pipe(pipefd));
417
418 size_t without_sharing, with_sharing;
419 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
420 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
421
422 // We expect the sharing to save at least 10% of the total PSS. In practice
423 // it saves 40%+ for this test.
424 size_t expected_size = without_sharing - (without_sharing/10);
425 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800426
427 // Use destructor of tf to close and unlink the file.
428 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100429}
430
431void getPss(pid_t pid, size_t* pss_out) {
432 pm_kernel_t* kernel;
433 ASSERT_EQ(0, pm_kernel_create(&kernel));
434
435 pm_process_t* process;
436 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
437
438 pm_map_t** maps;
439 size_t num_maps;
440 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
441
442 size_t total_pss = 0;
443 for (size_t i = 0; i < num_maps; i++) {
444 pm_memusage_t usage;
445 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
446 total_pss += usage.pss;
447 }
448 *pss_out = total_pss;
449
450 free(maps);
451 pm_process_destroy(process);
452 pm_kernel_destroy(kernel);
453}
454
455void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
456 size_t* pss_out) {
457 const int CHILDREN = 20;
458
459 // Create children
460 pid_t childpid[CHILDREN];
461 int childpipe[CHILDREN];
462 for (int i=0; i<CHILDREN; ++i) {
463 char read_buf;
464 int child_done_pipe[2], parent_done_pipe[2];
465 ASSERT_NOERROR(pipe(child_done_pipe));
466 ASSERT_NOERROR(pipe(parent_done_pipe));
467
468 pid_t child = fork();
469 if (child == 0) {
470 // close the 'wrong' ends of the pipes in the child
471 close(child_done_pipe[0]);
472 close(parent_done_pipe[1]);
473
474 // open the library
475 void* handle;
476 if (share_relro) {
477 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
478 } else {
479 handle = dlopen(lib, RTLD_NOW);
480 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700481 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100482 fprintf(stderr, "in child: %s\n", dlerror());
483 exit(1);
484 }
485
486 // close write end of child_done_pipe to signal the parent that we're done.
487 close(child_done_pipe[1]);
488
489 // wait for the parent to close parent_done_pipe, then exit
490 read(parent_done_pipe[0], &read_buf, 1);
491 exit(0);
492 }
493
494 ASSERT_NOERROR(child);
495
496 // close the 'wrong' ends of the pipes in the parent
497 close(child_done_pipe[1]);
498 close(parent_done_pipe[0]);
499
500 // wait for the child to be done
501 read(child_done_pipe[0], &read_buf, 1);
502 close(child_done_pipe[0]);
503
504 // save the child's pid and the parent_done_pipe
505 childpid[i] = child;
506 childpipe[i] = parent_done_pipe[1];
507 }
508
509 // Sum the PSS of all the children
510 size_t total_pss = 0;
511 for (int i=0; i<CHILDREN; ++i) {
512 size_t child_pss;
513 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
514 total_pss += child_pss;
515 }
516 *pss_out = total_pss;
517
518 // Close pipes and wait for children to exit
519 for (int i=0; i<CHILDREN; ++i) {
520 ASSERT_NOERROR(close(childpipe[i]));
521 }
522 for (int i=0; i<CHILDREN; ++i) {
523 int status;
524 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
525 ASSERT_TRUE(WIFEXITED(status));
526 ASSERT_EQ(0, WEXITSTATUS(status));
527 }
528}