blob: 44b899e91a52b8277d1c7b6fee2d27bae10d4495 [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());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700117
118 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
119 ASSERT_DL_NOTNULL(taxicab_number);
120 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700121}
122
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700123TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700124 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700125
126 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700127 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700128 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700129 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700130
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700131 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700132 ASSERT_DL_NOTNULL(handle_);
133
134 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
135 ASSERT_DL_NOTNULL(f);
136 EXPECT_EQ(4, f());
137}
138
139TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700140 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700141 // lib_path is relative when $ANDROID_DATA is relative
142 char lib_realpath_buf[PATH_MAX];
143 ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
144 const std::string lib_realpath = std::string(lib_realpath_buf);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700145
146 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700147 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700148 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700149 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700150
151 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
152 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700153 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
154
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800155 // Test an address above 2^44, for http://b/18178121 .
156 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700157 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700158 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800159 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
160
161 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
162 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
163 ASSERT_TRUE(handle_ == nullptr);
164 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
165
166 extinfo.library_fd_offset = PAGE_SIZE;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700167 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800168 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700169 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700170
171 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700172}
173
174TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
175 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700176 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
177 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700178
179 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
180 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700181 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 -0700182}
183
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700184TEST(dlext, android_dlopen_ext_force_load_smoke) {
185 // 1. Open actual file
186 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
187 ASSERT_DL_NOTNULL(handle);
188 // 2. Open link with force_load flag set
189 android_dlextinfo extinfo;
190 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
191 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
192 ASSERT_DL_NOTNULL(handle2);
193 ASSERT_TRUE(handle != handle2);
194
195 dlclose(handle2);
196 dlclose(handle);
197}
198
199TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
200 // Check if soname lookup still returns already loaded library
201 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
202 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
203 ASSERT_DL_NOTNULL(handle);
204
205 android_dlextinfo extinfo;
206 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
207
208 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
209 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
210
211 ASSERT_DL_NOTNULL(handle2);
212 ASSERT_TRUE(handle == handle2);
213
214 dlclose(handle2);
215 dlclose(handle);
216}
217
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700218TEST(dlfcn, dlopen_from_zip_absolute_path) {
219 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
220
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700221 void* handle = dlopen((lib_path + "!/libdir/libdlext_test_fd.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700222 ASSERT_TRUE(handle != nullptr) << dlerror();
223
224 int (*fn)(void);
225 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
226 ASSERT_TRUE(fn != nullptr);
227 EXPECT_EQ(4, fn());
228
229 dlclose(handle);
230}
231
232TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700233 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700234
235 typedef void (*fn_t)(const char*);
236 fn_t android_update_LD_LIBRARY_PATH =
237 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
238
239 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
240
241 void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
242 ASSERT_TRUE(handle == nullptr);
243
244 android_update_LD_LIBRARY_PATH(lib_path.c_str());
245
246 handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
247 ASSERT_TRUE(handle != nullptr) << dlerror();
248
249 int (*fn)(void);
250 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
251 ASSERT_TRUE(fn != nullptr);
252 EXPECT_EQ(4, fn());
253
254 dlclose(handle);
255}
256
257
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000258TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700259 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000260 -1, 0);
261 ASSERT_TRUE(start != MAP_FAILED);
262 android_dlextinfo extinfo;
263 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
264 extinfo.reserved_addr = start;
265 extinfo.reserved_size = LIBSIZE;
266 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
267 ASSERT_DL_NOTNULL(handle_);
268 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
269 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700270 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000271 EXPECT_LT(reinterpret_cast<void*>(f),
272 reinterpret_cast<char*>(start) + LIBSIZE);
273 EXPECT_EQ(4, f());
274}
275
276TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700277 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000278 -1, 0);
279 ASSERT_TRUE(start != MAP_FAILED);
280 android_dlextinfo extinfo;
281 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
282 extinfo.reserved_addr = start;
283 extinfo.reserved_size = PAGE_SIZE;
284 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700285 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000286}
287
288TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700289 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000290 -1, 0);
291 ASSERT_TRUE(start != MAP_FAILED);
292 android_dlextinfo extinfo;
293 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
294 extinfo.reserved_addr = start;
295 extinfo.reserved_size = LIBSIZE;
296 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
297 ASSERT_DL_NOTNULL(handle_);
298 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
299 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700300 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000301 EXPECT_LT(reinterpret_cast<void*>(f),
302 reinterpret_cast<char*>(start) + LIBSIZE);
303 EXPECT_EQ(4, f());
304}
305
306TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700307 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000308 -1, 0);
309 ASSERT_TRUE(start != MAP_FAILED);
310 android_dlextinfo extinfo;
311 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
312 extinfo.reserved_addr = start;
313 extinfo.reserved_size = PAGE_SIZE;
314 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
315 ASSERT_DL_NOTNULL(handle_);
316 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
317 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700318 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
319 (reinterpret_cast<void*>(f) >=
320 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000321 EXPECT_EQ(4, f());
322}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000323
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100324class DlExtRelroSharingTest : public DlExtTest {
325protected:
326 virtual void SetUp() {
327 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700328 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100329 -1, 0);
330 ASSERT_TRUE(start != MAP_FAILED);
331 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
332 extinfo_.reserved_addr = start;
333 extinfo_.reserved_size = LIBSIZE;
334 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000335 }
336
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100337 virtual void TearDown() {
338 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100339 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000340
Yabin Cui294d1e22014-12-07 20:43:37 -0800341 void CreateRelroFile(const char* lib, const char* relro_file) {
342 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100343 ASSERT_NOERROR(relro_fd);
344
345 pid_t pid = fork();
346 if (pid == 0) {
347 // child process
348 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
349 extinfo_.relro_fd = relro_fd;
350 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700351 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100352 fprintf(stderr, "in child: %s\n", dlerror());
353 exit(1);
354 }
355 exit(0);
356 }
357
358 // continuing in parent
359 ASSERT_NOERROR(close(relro_fd));
360 ASSERT_NOERROR(pid);
361 int status;
362 ASSERT_EQ(pid, waitpid(pid, &status, 0));
363 ASSERT_TRUE(WIFEXITED(status));
364 ASSERT_EQ(0, WEXITSTATUS(status));
365
366 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800367 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100368 ASSERT_NOERROR(relro_fd);
369 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
370 extinfo_.relro_fd = relro_fd;
371 }
372
373 void TryUsingRelro(const char* lib) {
374 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
375 ASSERT_DL_NOTNULL(handle_);
376 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
377 ASSERT_DL_NOTNULL(f);
378 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700379
380 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
381 ASSERT_DL_NOTNULL(taxicab_number);
382 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100383 }
384
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100385 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
386
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100387 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100388};
389
390TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800391 TemporaryFile tf; // Use tf to get an unique filename.
392 ASSERT_NOERROR(close(tf.fd));
393
394 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100395 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800396
397 // Use destructor of tf to close and unlink the file.
398 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100399}
400
401TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800402 TemporaryFile tf; // // Use tf to get an unique filename.
403 ASSERT_NOERROR(close(tf.fd));
404
405 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100406 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800407
408 // Use destructor of tf to close and unlink the file.
409 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410}
411
412TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100413 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000414}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100415
416TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700417 if (geteuid() != 0) {
418 GTEST_LOG_(INFO) << "This test must be run as root.\n";
419 return;
420 }
421
Yabin Cui294d1e22014-12-07 20:43:37 -0800422 TemporaryFile tf; // Use tf to get an unique filename.
423 ASSERT_NOERROR(close(tf.fd));
424
425 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
426
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100427 int pipefd[2];
428 ASSERT_NOERROR(pipe(pipefd));
429
430 size_t without_sharing, with_sharing;
431 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
432 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
433
434 // We expect the sharing to save at least 10% of the total PSS. In practice
435 // it saves 40%+ for this test.
436 size_t expected_size = without_sharing - (without_sharing/10);
437 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800438
439 // Use destructor of tf to close and unlink the file.
440 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100441}
442
443void getPss(pid_t pid, size_t* pss_out) {
444 pm_kernel_t* kernel;
445 ASSERT_EQ(0, pm_kernel_create(&kernel));
446
447 pm_process_t* process;
448 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
449
450 pm_map_t** maps;
451 size_t num_maps;
452 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
453
454 size_t total_pss = 0;
455 for (size_t i = 0; i < num_maps; i++) {
456 pm_memusage_t usage;
457 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
458 total_pss += usage.pss;
459 }
460 *pss_out = total_pss;
461
462 free(maps);
463 pm_process_destroy(process);
464 pm_kernel_destroy(kernel);
465}
466
467void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
468 size_t* pss_out) {
469 const int CHILDREN = 20;
470
471 // Create children
472 pid_t childpid[CHILDREN];
473 int childpipe[CHILDREN];
474 for (int i=0; i<CHILDREN; ++i) {
475 char read_buf;
476 int child_done_pipe[2], parent_done_pipe[2];
477 ASSERT_NOERROR(pipe(child_done_pipe));
478 ASSERT_NOERROR(pipe(parent_done_pipe));
479
480 pid_t child = fork();
481 if (child == 0) {
482 // close the 'wrong' ends of the pipes in the child
483 close(child_done_pipe[0]);
484 close(parent_done_pipe[1]);
485
486 // open the library
487 void* handle;
488 if (share_relro) {
489 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
490 } else {
491 handle = dlopen(lib, RTLD_NOW);
492 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700493 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100494 fprintf(stderr, "in child: %s\n", dlerror());
495 exit(1);
496 }
497
498 // close write end of child_done_pipe to signal the parent that we're done.
499 close(child_done_pipe[1]);
500
501 // wait for the parent to close parent_done_pipe, then exit
502 read(parent_done_pipe[0], &read_buf, 1);
503 exit(0);
504 }
505
506 ASSERT_NOERROR(child);
507
508 // close the 'wrong' ends of the pipes in the parent
509 close(child_done_pipe[1]);
510 close(parent_done_pipe[0]);
511
512 // wait for the child to be done
513 read(child_done_pipe[0], &read_buf, 1);
514 close(child_done_pipe[0]);
515
516 // save the child's pid and the parent_done_pipe
517 childpid[i] = child;
518 childpipe[i] = parent_done_pipe[1];
519 }
520
521 // Sum the PSS of all the children
522 size_t total_pss = 0;
523 for (int i=0; i<CHILDREN; ++i) {
524 size_t child_pss;
525 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
526 total_pss += child_pss;
527 }
528 *pss_out = total_pss;
529
530 // Close pipes and wait for children to exit
531 for (int i=0; i<CHILDREN; ++i) {
532 ASSERT_NOERROR(close(childpipe[i]));
533 }
534 for (int i=0; i<CHILDREN; ++i) {
535 int status;
536 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
537 ASSERT_TRUE(WIFEXITED(status));
538 ASSERT_EQ(0, WEXITSTATUS(status));
539 }
540}