2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
27 #include <android/dlext.h>
29 #include <sys/types.h>
32 #include <pagemap/pagemap.h>
34 #include "TemporaryFile.h"
36 #define ASSERT_DL_NOTNULL(ptr) \
37 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
39 #define ASSERT_DL_ZERO(i) \
40 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
42 #define ASSERT_NOERROR(i) \
43 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
45 #define ASSERT_SUBSTR(needle, haystack) \
46 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
49 typedef int (*fn)(void);
50 #define LIBNAME "libdlext_test.so"
51 #define LIBNAME_NORELRO "libdlext_test_norelro.so"
52 #define LIBSIZE 1024*1024 // how much address space to reserve for it
55 #define LIBPATH_PREFIX "/nativetest64/libdlext_test_fd/"
57 #define LIBPATH_PREFIX "/nativetest/libdlext_test_fd/"
60 #define LIBPATH LIBPATH_PREFIX "libdlext_test_fd.so"
61 #define LIBZIPPATH LIBPATH_PREFIX "libdlext_test_fd_zipaligned.zip"
63 #define LIBZIP_OFFSET 2*PAGE_SIZE
65 class DlExtTest : public ::testing::Test {
67 virtual void SetUp() {
69 // verify that we don't have the library loaded already
70 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);
74 // call dlerror() to swallow the error, and check it was the one we wanted
75 ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
78 virtual void TearDown() {
79 if (handle_ != nullptr) {
80 ASSERT_DL_ZERO(dlclose(handle_));
87 TEST_F(DlExtTest, ExtInfoNull) {
88 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
89 ASSERT_DL_NOTNULL(handle_);
90 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
95 TEST_F(DlExtTest, ExtInfoNoFlags) {
96 android_dlextinfo extinfo;
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);
105 TEST_F(DlExtTest, ExtInfoUseFd) {
106 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
108 android_dlextinfo extinfo;
109 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
110 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
111 ASSERT_TRUE(extinfo.library_fd != -1);
112 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
113 ASSERT_DL_NOTNULL(handle_);
114 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
115 ASSERT_DL_NOTNULL(f);
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);
123 TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
124 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
126 android_dlextinfo extinfo;
127 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
128 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
129 extinfo.library_fd_offset = LIBZIP_OFFSET;
131 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
132 ASSERT_DL_NOTNULL(handle_);
134 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
135 ASSERT_DL_NOTNULL(f);
139 TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
140 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
141 // 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);
146 android_dlextinfo extinfo;
147 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
148 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
149 extinfo.library_fd_offset = 17;
151 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
152 ASSERT_TRUE(handle_ == nullptr);
153 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
155 // Test an address above 2^44, for http://b/18178121 .
156 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
157 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
158 ASSERT_TRUE(handle_ == nullptr);
159 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
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());
166 extinfo.library_fd_offset = PAGE_SIZE;
167 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
168 ASSERT_TRUE(handle_ == nullptr);
169 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
171 close(extinfo.library_fd);
174 TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
175 android_dlextinfo extinfo;
176 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
177 extinfo.library_fd_offset = LIBZIP_OFFSET;
179 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
180 ASSERT_TRUE(handle_ == nullptr);
181 ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror());
184 TEST(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);
199 TEST(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);
205 android_dlextinfo extinfo;
206 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
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);
211 ASSERT_DL_NOTNULL(handle2);
212 ASSERT_TRUE(handle == handle2);
218 TEST(dlfcn, dlopen_from_zip_absolute_path) {
219 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
221 void* handle = dlopen((lib_path + "!/libdir/libdlext_test_fd.so").c_str(), RTLD_NOW);
222 ASSERT_TRUE(handle != nullptr) << dlerror();
225 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
226 ASSERT_TRUE(fn != nullptr);
232 TEST(dlfcn, dlopen_from_zip_ld_library_path) {
233 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
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"));
239 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
241 void* handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
242 ASSERT_TRUE(handle == nullptr);
244 android_update_LD_LIBRARY_PATH(lib_path.c_str());
246 handle = dlopen("libdlext_test_fd.so", RTLD_NOW);
247 ASSERT_TRUE(handle != nullptr) << dlerror();
250 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
251 ASSERT_TRUE(fn != nullptr);
258 TEST_F(DlExtTest, Reserved) {
259 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
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);
270 EXPECT_GE(reinterpret_cast<void*>(f), start);
271 EXPECT_LT(reinterpret_cast<void*>(f),
272 reinterpret_cast<char*>(start) + LIBSIZE);
276 TEST_F(DlExtTest, ReservedTooSmall) {
277 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
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);
285 EXPECT_EQ(nullptr, handle_);
288 TEST_F(DlExtTest, ReservedHint) {
289 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
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);
300 EXPECT_GE(reinterpret_cast<void*>(f), start);
301 EXPECT_LT(reinterpret_cast<void*>(f),
302 reinterpret_cast<char*>(start) + LIBSIZE);
306 TEST_F(DlExtTest, ReservedHintTooSmall) {
307 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
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);
318 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
319 (reinterpret_cast<void*>(f) >=
320 reinterpret_cast<char*>(start) + PAGE_SIZE));
324 class DlExtRelroSharingTest : public DlExtTest {
326 virtual void SetUp() {
328 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
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;
337 virtual void TearDown() {
338 DlExtTest::TearDown();
341 void CreateRelroFile(const char* lib, const char* relro_file) {
342 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
343 ASSERT_NOERROR(relro_fd);
348 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
349 extinfo_.relro_fd = relro_fd;
350 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
351 if (handle == nullptr) {
352 fprintf(stderr, "in child: %s\n", dlerror());
358 // continuing in parent
359 ASSERT_NOERROR(close(relro_fd));
362 ASSERT_EQ(pid, waitpid(pid, &status, 0));
363 ASSERT_TRUE(WIFEXITED(status));
364 ASSERT_EQ(0, WEXITSTATUS(status));
366 // reopen file for reading so it can be used
367 relro_fd = open(relro_file, O_RDONLY);
368 ASSERT_NOERROR(relro_fd);
369 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
370 extinfo_.relro_fd = relro_fd;
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);
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);
385 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
387 android_dlextinfo extinfo_;
390 TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
391 TemporaryFile tf; // Use tf to get an unique filename.
392 ASSERT_NOERROR(close(tf.fd));
394 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
395 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
397 // Use destructor of tf to close and unlink the file.
398 tf.fd = extinfo_.relro_fd;
401 TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
402 TemporaryFile tf; // // Use tf to get an unique filename.
403 ASSERT_NOERROR(close(tf.fd));
405 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
406 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
408 // Use destructor of tf to close and unlink the file.
409 tf.fd = extinfo_.relro_fd;
412 TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
413 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
416 TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
417 if (geteuid() != 0) {
418 GTEST_LOG_(INFO) << "This test must be run as root.\n";
422 TemporaryFile tf; // Use tf to get an unique filename.
423 ASSERT_NOERROR(close(tf.fd));
425 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
428 ASSERT_NOERROR(pipe(pipefd));
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));
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);
439 // Use destructor of tf to close and unlink the file.
440 tf.fd = extinfo_.relro_fd;
443 void getPss(pid_t pid, size_t* pss_out) {
445 ASSERT_EQ(0, pm_kernel_create(&kernel));
447 pm_process_t* process;
448 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
452 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
454 size_t total_pss = 0;
455 for (size_t i = 0; i < num_maps; i++) {
457 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
458 total_pss += usage.pss;
460 *pss_out = total_pss;
463 pm_process_destroy(process);
464 pm_kernel_destroy(kernel);
467 void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
469 const int CHILDREN = 20;
472 pid_t childpid[CHILDREN];
473 int childpipe[CHILDREN];
474 for (int i=0; i<CHILDREN; ++i) {
476 int child_done_pipe[2], parent_done_pipe[2];
477 ASSERT_NOERROR(pipe(child_done_pipe));
478 ASSERT_NOERROR(pipe(parent_done_pipe));
480 pid_t child = fork();
482 // close the 'wrong' ends of the pipes in the child
483 close(child_done_pipe[0]);
484 close(parent_done_pipe[1]);
489 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
491 handle = dlopen(lib, RTLD_NOW);
493 if (handle == nullptr) {
494 fprintf(stderr, "in child: %s\n", dlerror());
498 // close write end of child_done_pipe to signal the parent that we're done.
499 close(child_done_pipe[1]);
501 // wait for the parent to close parent_done_pipe, then exit
502 read(parent_done_pipe[0], &read_buf, 1);
506 ASSERT_NOERROR(child);
508 // close the 'wrong' ends of the pipes in the parent
509 close(child_done_pipe[1]);
510 close(parent_done_pipe[0]);
512 // wait for the child to be done
513 read(child_done_pipe[0], &read_buf, 1);
514 close(child_done_pipe[0]);
516 // save the child's pid and the parent_done_pipe
518 childpipe[i] = parent_done_pipe[1];
521 // Sum the PSS of all the children
522 size_t total_pss = 0;
523 for (int i=0; i<CHILDREN; ++i) {
525 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
526 total_pss += child_pss;
528 *pss_out = total_pss;
530 // Close pipes and wait for children to exit
531 for (int i=0; i<CHILDREN; ++i) {
532 ASSERT_NOERROR(close(childpipe[i]));
534 for (int i=0; i<CHILDREN; ++i) {
536 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
537 ASSERT_TRUE(WIFEXITED(status));
538 ASSERT_EQ(0, WEXITSTATUS(status));