blob: d5a5e562bd9ed96e709c37f578b05734f92cd860 [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 Ivanovb4827502015-09-28 16:38:31 -070055#define LIBPATH_PREFIX "/nativetest64/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070056#else
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070057#define LIBPATH_PREFIX "/nativetest/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070058#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000059
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070060#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 Ivanova1feb112015-10-01 18:41:57 -070062#define LIBZIPPATH_WITH_RUNPATH LIBPATH_PREFIX "libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070063
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070064#define LIBZIP_OFFSET PAGE_SIZE
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070065
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000066class DlExtTest : public ::testing::Test {
67protected:
68 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070069 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000070 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070071 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)12bbb912014-02-06 14:34:21 +000075 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070076 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 +000077 }
78
79 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070080 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000081 ASSERT_DL_ZERO(dlclose(handle_));
82 }
83 }
84
85 void* handle_;
86};
87
88TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070089 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000090 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
96TEST_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 Ivanov04dc91a2014-07-01 14:10:16 -0700106TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700107 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108
109 android_dlextinfo extinfo;
110 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700111 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700112 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700113 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700114 ASSERT_DL_NOTNULL(handle_);
115 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
116 ASSERT_DL_NOTNULL(f);
117 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700118
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 Ivanov04dc91a2014-07-01 14:10:16 -0700122}
123
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700124TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700125 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700126
127 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700128 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700129 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700130 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700131
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700132 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700133 ASSERT_DL_NOTNULL(handle_);
134
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700135 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 Ivanov07e5bc12014-10-03 17:52:44 -0700138}
139
140TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700141 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700142 // 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 Ivanov07e5bc12014-10-03 17:52:44 -0700146
147 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700148 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700149 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700150 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700151
152 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
153 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700154 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
155
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800156 // Test an address above 2^44, for http://b/18178121 .
157 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700158 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800160 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 Ivanovb4827502015-09-28 16:38:31 -0700167 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700168 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800169 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700170 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171
172 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700173}
174
175TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
176 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700177 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
178 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700179
180 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
181 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182 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 -0700183}
184
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700185TEST(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
200TEST(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 Ivanov52393a52015-03-18 22:50:01 -0700219TEST(dlfcn, dlopen_from_zip_absolute_path) {
220 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
221
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700222 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700223 ASSERT_TRUE(handle != nullptr) << dlerror();
224
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700225 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 Ivanov52393a52015-03-18 22:50:01 -0700228
229 dlclose(handle);
230}
231
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700232TEST(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 Ivanov52393a52015-03-18 22:50:01 -0700250TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700251 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700252
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 Ivanovb4827502015-09-28 16:38:31 -0700259 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700260 ASSERT_TRUE(handle == nullptr);
261
262 android_update_LD_LIBRARY_PATH(lib_path.c_str());
263
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700264 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700265 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 Ivanovb4827502015-09-28 16:38:31 -0700272 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 Ivanov52393a52015-03-18 22:50:01 -0700276 dlclose(handle);
277}
278
279
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000280TEST_F(DlExtTest, Reserved) {
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;
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, ReservedTooSmall) {
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;
304 extinfo.reserved_addr = start;
305 extinfo.reserved_size = PAGE_SIZE;
306 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700307 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000308}
309
310TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700311 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000312 -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 Hsieha2c6ae62014-08-27 13:45:37 -0700322 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000323 EXPECT_LT(reinterpret_cast<void*>(f),
324 reinterpret_cast<char*>(start) + LIBSIZE);
325 EXPECT_EQ(4, f());
326}
327
328TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700329 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000330 -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 Hsieha2c6ae62014-08-27 13:45:37 -0700340 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
341 (reinterpret_cast<void*>(f) >=
342 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000343 EXPECT_EQ(4, f());
344}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000345
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100346class DlExtRelroSharingTest : public DlExtTest {
347protected:
348 virtual void SetUp() {
349 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700350 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100351 -1, 0);
352 ASSERT_TRUE(start != MAP_FAILED);
353 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
354 extinfo_.reserved_addr = start;
355 extinfo_.reserved_size = LIBSIZE;
356 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000357 }
358
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100359 virtual void TearDown() {
360 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100361 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000362
Yabin Cui294d1e22014-12-07 20:43:37 -0800363 void CreateRelroFile(const char* lib, const char* relro_file) {
364 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100365 ASSERT_NOERROR(relro_fd);
366
367 pid_t pid = fork();
368 if (pid == 0) {
369 // child process
370 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
371 extinfo_.relro_fd = relro_fd;
372 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700373 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100374 fprintf(stderr, "in child: %s\n", dlerror());
375 exit(1);
376 }
377 exit(0);
378 }
379
380 // continuing in parent
381 ASSERT_NOERROR(close(relro_fd));
382 ASSERT_NOERROR(pid);
383 int status;
384 ASSERT_EQ(pid, waitpid(pid, &status, 0));
385 ASSERT_TRUE(WIFEXITED(status));
386 ASSERT_EQ(0, WEXITSTATUS(status));
387
388 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800389 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100390 ASSERT_NOERROR(relro_fd);
391 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
392 extinfo_.relro_fd = relro_fd;
393 }
394
395 void TryUsingRelro(const char* lib) {
396 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
397 ASSERT_DL_NOTNULL(handle_);
398 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
399 ASSERT_DL_NOTNULL(f);
400 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700401
402 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
403 ASSERT_DL_NOTNULL(taxicab_number);
404 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100405 }
406
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100407 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
408
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100409 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410};
411
412TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800413 TemporaryFile tf; // Use tf to get an unique filename.
414 ASSERT_NOERROR(close(tf.fd));
415
416 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100417 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800418
419 // Use destructor of tf to close and unlink the file.
420 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100421}
422
423TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800424 TemporaryFile tf; // // Use tf to get an unique filename.
425 ASSERT_NOERROR(close(tf.fd));
426
427 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100428 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800429
430 // Use destructor of tf to close and unlink the file.
431 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100432}
433
434TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100435 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000436}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100437
438TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700439 if (geteuid() != 0) {
440 GTEST_LOG_(INFO) << "This test must be run as root.\n";
441 return;
442 }
443
Yabin Cui294d1e22014-12-07 20:43:37 -0800444 TemporaryFile tf; // Use tf to get an unique filename.
445 ASSERT_NOERROR(close(tf.fd));
446
447 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
448
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100449 int pipefd[2];
450 ASSERT_NOERROR(pipe(pipefd));
451
452 size_t without_sharing, with_sharing;
453 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
454 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
455
456 // We expect the sharing to save at least 10% of the total PSS. In practice
457 // it saves 40%+ for this test.
458 size_t expected_size = without_sharing - (without_sharing/10);
459 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800460
461 // Use destructor of tf to close and unlink the file.
462 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100463}
464
465void getPss(pid_t pid, size_t* pss_out) {
466 pm_kernel_t* kernel;
467 ASSERT_EQ(0, pm_kernel_create(&kernel));
468
469 pm_process_t* process;
470 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
471
472 pm_map_t** maps;
473 size_t num_maps;
474 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
475
476 size_t total_pss = 0;
477 for (size_t i = 0; i < num_maps; i++) {
478 pm_memusage_t usage;
479 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
480 total_pss += usage.pss;
481 }
482 *pss_out = total_pss;
483
484 free(maps);
485 pm_process_destroy(process);
486 pm_kernel_destroy(kernel);
487}
488
489void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
490 size_t* pss_out) {
491 const int CHILDREN = 20;
492
493 // Create children
494 pid_t childpid[CHILDREN];
495 int childpipe[CHILDREN];
496 for (int i=0; i<CHILDREN; ++i) {
497 char read_buf;
498 int child_done_pipe[2], parent_done_pipe[2];
499 ASSERT_NOERROR(pipe(child_done_pipe));
500 ASSERT_NOERROR(pipe(parent_done_pipe));
501
502 pid_t child = fork();
503 if (child == 0) {
504 // close the 'wrong' ends of the pipes in the child
505 close(child_done_pipe[0]);
506 close(parent_done_pipe[1]);
507
508 // open the library
509 void* handle;
510 if (share_relro) {
511 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
512 } else {
513 handle = dlopen(lib, RTLD_NOW);
514 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700515 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100516 fprintf(stderr, "in child: %s\n", dlerror());
517 exit(1);
518 }
519
520 // close write end of child_done_pipe to signal the parent that we're done.
521 close(child_done_pipe[1]);
522
523 // wait for the parent to close parent_done_pipe, then exit
524 read(parent_done_pipe[0], &read_buf, 1);
525 exit(0);
526 }
527
528 ASSERT_NOERROR(child);
529
530 // close the 'wrong' ends of the pipes in the parent
531 close(child_done_pipe[1]);
532 close(parent_done_pipe[0]);
533
534 // wait for the child to be done
535 read(child_done_pipe[0], &read_buf, 1);
536 close(child_done_pipe[0]);
537
538 // save the child's pid and the parent_done_pipe
539 childpid[i] = child;
540 childpipe[i] = parent_done_pipe[1];
541 }
542
543 // Sum the PSS of all the children
544 size_t total_pss = 0;
545 for (int i=0; i<CHILDREN; ++i) {
546 size_t child_pss;
547 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
548 total_pss += child_pss;
549 }
550 *pss_out = total_pss;
551
552 // Close pipes and wait for children to exit
553 for (int i=0; i<CHILDREN; ++i) {
554 ASSERT_NOERROR(close(childpipe[i]));
555 }
556 for (int i=0; i<CHILDREN; ++i) {
557 int status;
558 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
559 ASSERT_TRUE(WIFEXITED(status));
560 ASSERT_EQ(0, WEXITSTATUS(status));
561 }
562}