blob: 17e1a48cfaa706e3b14d3ea64919c359caa048b5 [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>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080033#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010034
Dimitry Ivanov927877c2016-09-21 11:17:13 -070035#include "gtest_globals.h"
Yabin Cui294d1e22014-12-07 20:43:37 -080036#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080037#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070038#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070039#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000040
41#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070042 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000043
44#define ASSERT_DL_ZERO(i) \
45 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
46
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000047#define ASSERT_NOERROR(i) \
48 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
49
Yabin Cui16f7f8d2014-11-04 11:08:05 -080050#define ASSERT_SUBSTR(needle, haystack) \
51 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
52
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053
54typedef int (*fn)(void);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070055constexpr const char* kLibName = "libdlext_test.so";
56constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
57constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
58constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070059
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000060class DlExtTest : public ::testing::Test {
61protected:
62 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070063 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000064 // verify that we don't have the library loaded already
Dimitry Ivanov927877c2016-09-21 11:17:13 -070065 void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070066 ASSERT_TRUE(h == nullptr);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070067 h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070068 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000069 // call dlerror() to swallow the error, and check it was the one we wanted
Dimitry Ivanov927877c2016-09-21 11:17:13 -070070 ASSERT_EQ(std::string("dlopen failed: library \"") + kLibNameNoRelro + "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000071 }
72
73 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070074 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000075 ASSERT_DL_ZERO(dlclose(handle_));
76 }
77 }
78
79 void* handle_;
80};
81
82TEST_F(DlExtTest, ExtInfoNull) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -070083 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000084 ASSERT_DL_NOTNULL(handle_);
85 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
86 ASSERT_DL_NOTNULL(f);
87 EXPECT_EQ(4, f());
88}
89
90TEST_F(DlExtTest, ExtInfoNoFlags) {
91 android_dlextinfo extinfo;
92 extinfo.flags = 0;
Dimitry Ivanov927877c2016-09-21 11:17:13 -070093 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000094 ASSERT_DL_NOTNULL(handle_);
95 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
96 ASSERT_DL_NOTNULL(f);
97 EXPECT_EQ(4, f());
98}
99
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700100TEST_F(DlExtTest, ExtInfoUseFd) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800101 const std::string lib_path = get_testlib_root() + "/libdlext_test_fd/libdlext_test_fd.so";
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700102
103 android_dlextinfo extinfo;
104 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700105 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700106 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700107 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108 ASSERT_DL_NOTNULL(handle_);
109 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
110 ASSERT_DL_NOTNULL(f);
111 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700112
113 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
114 ASSERT_DL_NOTNULL(taxicab_number);
115 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700116}
117
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700118TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800119 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700120
121 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700122 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700123 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800124
125 // Find the offset of the shared library in the zip.
126 ZipArchiveHandle handle;
127 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
128 ZipEntry zip_entry;
129 ZipString zip_name;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700130 zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
131 zip_name.name_length = strlen(kLibZipSimpleZip);
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800132 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
133 extinfo.library_fd_offset = zip_entry.offset;
134 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700135
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700136 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700137 ASSERT_DL_NOTNULL(handle_);
138
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700139 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
140 ASSERT_DL_NOTNULL(taxicab_number);
141 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700142}
143
144TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800145 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
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);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700170 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800172 // Check if dlsym works after unsuccessful dlopen().
173 // Supply non-exiting one to make linker visit every soinfo.
174 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
175 ASSERT_TRUE(sym == nullptr);
176
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700177 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700178}
179
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800180TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700181 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800183 // This offset will not be used, so it doesn't matter.
184 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700185
186 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
187 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700188 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 -0700189}
190
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700191TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700192 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
193 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700194 // 1. Open actual file
195 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
196 ASSERT_DL_NOTNULL(handle);
197 // 2. Open link with force_load flag set
198 android_dlextinfo extinfo;
199 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700200 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700201 ASSERT_DL_NOTNULL(handle2);
202 ASSERT_TRUE(handle != handle2);
203
204 dlclose(handle2);
205 dlclose(handle);
206}
207
208TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700209 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
210 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700211 // Check if soname lookup still returns already loaded library
212 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700213 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700214 ASSERT_DL_NOTNULL(handle);
215
216 android_dlextinfo extinfo;
217 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
218
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700219 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700220 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
221
222 ASSERT_DL_NOTNULL(handle2);
223 ASSERT_TRUE(handle == handle2);
224
225 dlclose(handle2);
226 dlclose(handle);
227}
228
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700229TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700230 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800231 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700232
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700233 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700234 ASSERT_TRUE(handle != nullptr) << dlerror();
235
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700236 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
237 ASSERT_DL_NOTNULL(taxicab_number);
238 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700239
240 dlclose(handle);
241}
242
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700243TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700244 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800245 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700246
247 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
248
249 ASSERT_TRUE(handle != nullptr) << dlerror();
250
251 typedef void *(* dlopen_b_fn)();
252 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
253 ASSERT_TRUE(fn != nullptr) << dlerror();
254
255 void *p = fn();
256 ASSERT_TRUE(p != nullptr) << dlerror();
257
258 dlclose(p);
259 dlclose(handle);
260}
261
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700262TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700263 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800264 const std::string lib_path = get_testlib_root() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700265
266 typedef void (*fn_t)(const char*);
267 fn_t android_update_LD_LIBRARY_PATH =
268 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
269
270 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
271
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700272 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700273 ASSERT_TRUE(handle == nullptr);
274
275 android_update_LD_LIBRARY_PATH(lib_path.c_str());
276
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700277 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700278 ASSERT_TRUE(handle != nullptr) << dlerror();
279
280 int (*fn)(void);
281 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
282 ASSERT_TRUE(fn != nullptr);
283 EXPECT_EQ(4, fn());
284
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800285 uint32_t* taxicab_number =
286 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700287 ASSERT_DL_NOTNULL(taxicab_number);
288 EXPECT_EQ(1729U, *taxicab_number);
289
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700290 dlclose(handle);
291}
292
293
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000294TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700295 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000296 ASSERT_TRUE(start != MAP_FAILED);
297 android_dlextinfo extinfo;
298 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
299 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700300 extinfo.reserved_size = kLibSize;
301 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000302 ASSERT_DL_NOTNULL(handle_);
303 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
304 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700305 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000306 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700307 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000308 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800309
310 // Check that after dlclose reserved address space is unmapped (and can be reused)
311 dlclose(handle_);
312 handle_ = nullptr;
313
314 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
315 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000316}
317
318TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800319 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000320 ASSERT_TRUE(start != MAP_FAILED);
321 android_dlextinfo extinfo;
322 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
323 extinfo.reserved_addr = start;
324 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700325 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700326 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000327}
328
329TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700330 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000331 ASSERT_TRUE(start != MAP_FAILED);
332 android_dlextinfo extinfo;
333 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
334 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700335 extinfo.reserved_size = kLibSize;
336 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000337 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_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000341 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700342 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000343 EXPECT_EQ(4, f());
344}
345
346TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800347 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000348 ASSERT_TRUE(start != MAP_FAILED);
349 android_dlextinfo extinfo;
350 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
351 extinfo.reserved_addr = start;
352 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700353 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000354 ASSERT_DL_NOTNULL(handle_);
355 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
356 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700357 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
358 (reinterpret_cast<void*>(f) >=
359 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000360 EXPECT_EQ(4, f());
361}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000362
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700363TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700364 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700365 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700366 munmap(start, kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700367
368 android_dlextinfo extinfo;
369 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
370 extinfo.reserved_addr = start;
371
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700372 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700373 ASSERT_DL_NOTNULL(handle_);
374 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
375 ASSERT_DL_NOTNULL(f);
376 EXPECT_GE(reinterpret_cast<void*>(f), start);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700377 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700378
379 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800380 dlclose(handle_);
381 handle_ = nullptr;
382
383 // Check that dlclose unmapped the file
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700384 void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800385 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700386}
387
388TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700389 void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700390 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
391 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700392 munmap(start, kLibSize + PAGE_SIZE);
393 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700394 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
395 ASSERT_TRUE(new_addr != MAP_FAILED);
396
397 android_dlextinfo extinfo;
398 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
399 extinfo.reserved_addr = start;
400
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700401 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700402 ASSERT_TRUE(handle_ == nullptr);
403}
404
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100405class DlExtRelroSharingTest : public DlExtTest {
406protected:
407 virtual void SetUp() {
408 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700409 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410 ASSERT_TRUE(start != MAP_FAILED);
411 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
412 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700413 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100414 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000415 }
416
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100417 virtual void TearDown() {
418 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100419 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000420
Yabin Cui294d1e22014-12-07 20:43:37 -0800421 void CreateRelroFile(const char* lib, const char* relro_file) {
422 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100423 ASSERT_NOERROR(relro_fd);
424
425 pid_t pid = fork();
426 if (pid == 0) {
427 // child process
428 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
429 extinfo_.relro_fd = relro_fd;
430 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700431 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100432 fprintf(stderr, "in child: %s\n", dlerror());
433 exit(1);
434 }
435 exit(0);
436 }
437
438 // continuing in parent
439 ASSERT_NOERROR(close(relro_fd));
440 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800441 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100442
443 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800444 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100445 ASSERT_NOERROR(relro_fd);
446 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
447 extinfo_.relro_fd = relro_fd;
448 }
449
450 void TryUsingRelro(const char* lib) {
451 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
452 ASSERT_DL_NOTNULL(handle_);
453 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
454 ASSERT_DL_NOTNULL(f);
455 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700456
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800457 uint32_t* taxicab_number =
458 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700459 ASSERT_DL_NOTNULL(taxicab_number);
460 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100461 }
462
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100463 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
464
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100465 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100466};
467
468TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800469 TemporaryFile tf; // Use tf to get an unique filename.
470 ASSERT_NOERROR(close(tf.fd));
471
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700472 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
473 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Yabin Cui294d1e22014-12-07 20:43:37 -0800474
475 // Use destructor of tf to close and unlink the file.
476 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100477}
478
479TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800480 TemporaryFile tf; // // Use tf to get an unique filename.
481 ASSERT_NOERROR(close(tf.fd));
482
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700483 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
484 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
Yabin Cui294d1e22014-12-07 20:43:37 -0800485
486 // Use destructor of tf to close and unlink the file.
487 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100488}
489
490TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700491 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000492}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100493
494TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700495 if (geteuid() != 0) {
496 GTEST_LOG_(INFO) << "This test must be run as root.\n";
497 return;
498 }
499
Yabin Cui294d1e22014-12-07 20:43:37 -0800500 TemporaryFile tf; // Use tf to get an unique filename.
501 ASSERT_NOERROR(close(tf.fd));
502
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700503 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
Yabin Cui294d1e22014-12-07 20:43:37 -0800504
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100505 int pipefd[2];
506 ASSERT_NOERROR(pipe(pipefd));
507
508 size_t without_sharing, with_sharing;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700509 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, false, &without_sharing));
510 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, true, &with_sharing));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100511
512 // We expect the sharing to save at least 10% of the total PSS. In practice
513 // it saves 40%+ for this test.
514 size_t expected_size = without_sharing - (without_sharing/10);
515 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800516
517 // Use destructor of tf to close and unlink the file.
518 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100519}
520
521void getPss(pid_t pid, size_t* pss_out) {
522 pm_kernel_t* kernel;
523 ASSERT_EQ(0, pm_kernel_create(&kernel));
524
525 pm_process_t* process;
526 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
527
528 pm_map_t** maps;
529 size_t num_maps;
530 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
531
532 size_t total_pss = 0;
533 for (size_t i = 0; i < num_maps; i++) {
534 pm_memusage_t usage;
535 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
536 total_pss += usage.pss;
537 }
538 *pss_out = total_pss;
539
540 free(maps);
541 pm_process_destroy(process);
542 pm_kernel_destroy(kernel);
543}
544
545void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
546 size_t* pss_out) {
547 const int CHILDREN = 20;
548
549 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800550 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100551 int childpipe[CHILDREN];
552 for (int i=0; i<CHILDREN; ++i) {
553 char read_buf;
554 int child_done_pipe[2], parent_done_pipe[2];
555 ASSERT_NOERROR(pipe(child_done_pipe));
556 ASSERT_NOERROR(pipe(parent_done_pipe));
557
558 pid_t child = fork();
559 if (child == 0) {
560 // close the 'wrong' ends of the pipes in the child
561 close(child_done_pipe[0]);
562 close(parent_done_pipe[1]);
563
564 // open the library
565 void* handle;
566 if (share_relro) {
567 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
568 } else {
569 handle = dlopen(lib, RTLD_NOW);
570 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700571 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100572 fprintf(stderr, "in child: %s\n", dlerror());
573 exit(1);
574 }
575
576 // close write end of child_done_pipe to signal the parent that we're done.
577 close(child_done_pipe[1]);
578
579 // wait for the parent to close parent_done_pipe, then exit
580 read(parent_done_pipe[0], &read_buf, 1);
581 exit(0);
582 }
583
584 ASSERT_NOERROR(child);
585
586 // close the 'wrong' ends of the pipes in the parent
587 close(child_done_pipe[1]);
588 close(parent_done_pipe[0]);
589
590 // wait for the child to be done
591 read(child_done_pipe[0], &read_buf, 1);
592 close(child_done_pipe[0]);
593
594 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800595 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100596 childpipe[i] = parent_done_pipe[1];
597 }
598
599 // Sum the PSS of all the children
600 size_t total_pss = 0;
601 for (int i=0; i<CHILDREN; ++i) {
602 size_t child_pss;
Elliott Hughes33697a02016-01-26 13:04:57 -0800603 ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100604 total_pss += child_pss;
605 }
606 *pss_out = total_pss;
607
608 // Close pipes and wait for children to exit
609 for (int i=0; i<CHILDREN; ++i) {
610 ASSERT_NOERROR(close(childpipe[i]));
611 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800612 for (int i = 0; i < CHILDREN; ++i) {
613 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100614 }
615}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700616
617// Testing namespaces
618static const char* g_public_lib = "libnstest_public.so";
619
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800620// These are libs shared with default namespace
621static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
622
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700623TEST(dlext, ns_smoke) {
624 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800625 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700626
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800627 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
628 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
629 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
630 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700631
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800632 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800633 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700634 ASSERT_TRUE(handle_public != nullptr) << dlerror();
635
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800636 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700637
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800638 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700639 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800640 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800641 ASSERT_TRUE(handle_public == nullptr);
642 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
643 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
644
645 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700646
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800647 // create "public namespace", share limited set of public libraries with
648
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800649 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800650 android_create_namespace("private",
651 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800652 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800653 ANDROID_NAMESPACE_TYPE_REGULAR,
654 nullptr,
655 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700656 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800657 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700658
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800659 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800660 android_create_namespace("private_isolated",
661 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800662 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800663 ANDROID_NAMESPACE_TYPE_ISOLATED,
664 nullptr,
665 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700666 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800667 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700668
669 // This should not have affect search path for default namespace:
670 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
671 void* handle = dlopen(g_public_lib, RTLD_NOW);
672 ASSERT_TRUE(handle != nullptr) << dlerror();
673 dlclose(handle);
674
675 android_dlextinfo extinfo;
676 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
677 extinfo.library_namespace = ns1;
678
679 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
680 ASSERT_TRUE(handle1 != nullptr) << dlerror();
681
682 extinfo.library_namespace = ns2;
683 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
684 ASSERT_TRUE(handle2 != nullptr) << dlerror();
685
686 ASSERT_TRUE(handle1 != handle2);
687
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800688 // dlopen for a public library using an absolute path should work for isolated namespaces
689 extinfo.library_namespace = ns2;
690 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
691 ASSERT_TRUE(handle != nullptr) << dlerror();
692 ASSERT_TRUE(handle == handle_public);
693
694 dlclose(handle);
695
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700696 typedef const char* (*fn_t)();
697
698 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
699 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
700 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
701 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
702
703 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
704 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
705
706 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
707
708 fn_t ns_get_private_extern_string1 =
709 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
710 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
711 fn_t ns_get_private_extern_string2 =
712 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
713 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
714
715 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
716 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
717
718 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
719
720 fn_t ns_get_public_extern_string1 =
721 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
722 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
723 fn_t ns_get_public_extern_string2 =
724 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
725 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
726
727 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
728 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
729
730 // and now check that dlopen() does the right thing in terms of preserving namespace
731 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
732 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
733 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
734 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
735
736 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
737 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
738
739 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
740
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800741 // Check that symbols from non-shared libraries a shared library depends on are not visible
742 // from original namespace.
743
744 fn_t ns_get_internal_extern_string =
745 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
746 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
747 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
748 "ns_get_internal_extern_string() expected to return null but returns \"" <<
749 ns_get_internal_extern_string() << "\"";
750
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700751 dlclose(handle1);
752
753 // Check if handle2 is still alive (and well)
754 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
755 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
756 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
757 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
758
759 dlclose(handle2);
760}
761
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800762TEST(dlext, ns_symbol_visibilty_one_namespace) {
763 static const char* root_lib = "libnstest_root.so";
764 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
765
766 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs:" +
767 get_testlib_root() + "/private_namespace_libs";
768
769 android_namespace_t* ns =
770 android_create_namespace("one",
771 nullptr,
772 ns_search_path.c_str(),
773 ANDROID_NAMESPACE_TYPE_ISOLATED,
774 nullptr,
775 nullptr);
776
777 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
778
779 android_dlextinfo extinfo;
780 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
781 extinfo.library_namespace = ns;
782
783 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
784 ASSERT_TRUE(handle != nullptr) << dlerror();
785
786 typedef const char* (*fn_t)();
787
788 // Check that relocation worked correctly
789 fn_t ns_get_internal_extern_string =
790 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
791 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
792 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
793
794 fn_t internal_extern_string_fn =
795 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
796 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
797 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
798}
799
800TEST(dlext, ns_symbol_visibilty_between_namespaces) {
801 static const char* root_lib = "libnstest_root.so";
802 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
803
804 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
805 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
806
807 android_namespace_t* ns_public =
808 android_create_namespace("public",
809 nullptr,
810 public_ns_search_path.c_str(),
811 ANDROID_NAMESPACE_TYPE_ISOLATED,
812 nullptr,
813 nullptr);
814
815 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
816
817 android_namespace_t* ns_private =
818 android_create_namespace("private",
819 nullptr,
820 private_ns_search_path.c_str(),
821 ANDROID_NAMESPACE_TYPE_ISOLATED,
822 nullptr,
823 nullptr);
824
825 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
826 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
827
828 android_dlextinfo extinfo;
829 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
830 extinfo.library_namespace = ns_private;
831
832 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
833 ASSERT_TRUE(handle != nullptr) << dlerror();
834
835 typedef const char* (*fn_t)();
836
837 // Check that relocation worked correctly
838 fn_t ns_get_internal_extern_string =
839 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
840 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
841 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
842 "ns_get_internal_extern_string() expected to return null but returns \"" <<
843 ns_get_internal_extern_string() << "\"";
844
845 fn_t internal_extern_string_fn =
846 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
847 ASSERT_TRUE(internal_extern_string_fn == nullptr);
848 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
849}
850
851TEST(dlext, ns_unload_between_namespaces) {
852 static const char* root_lib = "libnstest_root.so";
853 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
854
855 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
856 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
857
858 android_namespace_t* ns_public =
859 android_create_namespace("public",
860 nullptr,
861 public_ns_search_path.c_str(),
862 ANDROID_NAMESPACE_TYPE_ISOLATED,
863 nullptr,
864 nullptr);
865
866 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
867
868 android_namespace_t* ns_private =
869 android_create_namespace("private",
870 nullptr,
871 private_ns_search_path.c_str(),
872 ANDROID_NAMESPACE_TYPE_ISOLATED,
873 nullptr,
874 nullptr);
875
876 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
877 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
878
879 android_dlextinfo extinfo;
880 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
881 extinfo.library_namespace = ns_private;
882
883 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
884 ASSERT_TRUE(handle != nullptr) << dlerror();
885
886 dlclose(handle);
887 // Check that root_lib was unloaded
888 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
889 ASSERT_TRUE(handle == nullptr);
890 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
891 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
892
893 // Check that shared library was unloaded in public ns
894 extinfo.library_namespace = ns_public;
895 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
896 ASSERT_TRUE(handle == nullptr);
897 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
898 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
899}
900
Dimitry Ivanov18623142017-02-21 13:41:08 -0800901TEST(dlext, ns_greylist) {
902 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
903
904 const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
905
906 android_namespace_t* ns =
907 android_create_namespace("namespace",
908 nullptr,
909 ns_search_path.c_str(),
910 ANDROID_NAMESPACE_TYPE_ISOLATED,
911 nullptr,
912 nullptr);
913
914 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
915
916 android_dlextinfo extinfo;
917 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
918 extinfo.library_namespace = ns;
919
920 android_set_application_target_sdk_version(__ANDROID_API_M__);
921 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
922 ASSERT_TRUE(handle != nullptr) << dlerror();
923
924 dlclose(handle);
925
926 android_set_application_target_sdk_version(__ANDROID_API_N__);
927 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
928 ASSERT_TRUE(handle == nullptr);
929 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
930}
931
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800932TEST(dlext, ns_cyclic_namespaces) {
933 // Test that ns1->ns2->ns1 link does not break the loader
934 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
935 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
936
937 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs";
938
939 android_namespace_t* ns1 =
940 android_create_namespace("ns1",
941 nullptr,
942 ns_search_path.c_str(),
943 ANDROID_NAMESPACE_TYPE_ISOLATED,
944 nullptr,
945 nullptr);
946
947 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
948
949 android_namespace_t* ns2 =
950 android_create_namespace("ns1",
951 nullptr,
952 ns_search_path.c_str(),
953 ANDROID_NAMESPACE_TYPE_ISOLATED,
954 nullptr,
955 nullptr);
956
957 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
958
959 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
960 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
961
962 android_dlextinfo extinfo;
963 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
964 extinfo.library_namespace = ns1;
965
966 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
967 ASSERT_TRUE(handle == nullptr);
968 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
969}
970
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700971TEST(dlext, ns_isolated) {
972 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800973 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700974
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800975 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800976 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700977 ASSERT_TRUE(handle_public != nullptr) << dlerror();
978
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800979 android_set_application_target_sdk_version(42U); // something > 23
980
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800981 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700982
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800983 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800984 android_create_namespace("private",
985 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800986 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800987 ANDROID_NAMESPACE_TYPE_REGULAR,
988 nullptr,
989 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700990 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800991 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700992
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800993 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700994 android_create_namespace("private_isolated1",
995 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800996 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700997 ANDROID_NAMESPACE_TYPE_ISOLATED,
998 nullptr,
999 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001000 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001001 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001002
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001003 android_namespace_t* ns_isolated2 =
1004 android_create_namespace("private_isolated2",
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001005 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001006 nullptr,
1007 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001008 get_testlib_root().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001009 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001010 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001011 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001012
1013 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1014 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1015
1016 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001017 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001018
1019 // Load lib_private_external_path to default namespace
1020 // (it should remain invisible for the isolated namespaces after this)
1021 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1022 ASSERT_TRUE(handle != nullptr) << dlerror();
1023
1024 android_dlextinfo extinfo;
1025 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1026 extinfo.library_namespace = ns_not_isolated;
1027
1028 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1029 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1030
1031 extinfo.library_namespace = ns_isolated;
1032
1033 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1034 ASSERT_TRUE(handle2 == nullptr);
1035 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1036
1037 // Check dlopen by absolute path
1038 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1039 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001040 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001041 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001042 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001043
1044 extinfo.library_namespace = ns_isolated2;
1045
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001046 // this should work because isolation_path for private_isolated2 includes get_testlib_root()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001047 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001048 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1049 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001050
1051 // Check dlopen by absolute path
1052 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001053 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1054 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001055
1056 typedef const char* (*fn_t)();
1057 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1058 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1059
1060 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1061
1062 fn_t ns_get_private_extern_string =
1063 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1064 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1065
1066 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1067
1068 fn_t ns_get_public_extern_string =
1069 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1070 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1071
1072 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1073
1074 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1075 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1076
1077 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1078
1079 dlclose(handle1);
1080}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001081
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001082TEST(dlext, ns_shared) {
1083 static const char* root_lib = "libnstest_root_not_isolated.so";
1084 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001085
1086 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001087
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001088 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001089 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1090 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1091
1092 android_set_application_target_sdk_version(42U); // something > 23
1093
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001094 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001095
1096 // preload this library to the default namespace to check if it
1097 // is shared later on.
1098 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001099 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001100 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1101
1102 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001103 android_create_namespace("private",
1104 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001105 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001106 ANDROID_NAMESPACE_TYPE_REGULAR,
1107 nullptr,
1108 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001109 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001110 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001111
1112 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001113 android_create_namespace("private_isolated_shared",
1114 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001115 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001116 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001117 nullptr,
1118 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001119 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001120 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001121
1122 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1123 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1124
1125 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001126 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001127
1128 // Load lib_private_external_path to default namespace
1129 // (it should remain invisible for the isolated namespaces after this)
1130 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1131 ASSERT_TRUE(handle != nullptr) << dlerror();
1132
1133 android_dlextinfo extinfo;
1134 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1135 extinfo.library_namespace = ns_not_isolated;
1136
1137 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1138 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1139
1140 extinfo.library_namespace = ns_isolated_shared;
1141
1142 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1143 ASSERT_TRUE(handle2 == nullptr);
1144 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1145
1146 // Check dlopen by absolute path
1147 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1148 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001149 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001150 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001151 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001152
1153 // load libnstest_root.so to shared namespace in order to check that everything is different
1154 // except shared libnstest_dlopened.so
1155
1156 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1157
1158 typedef const char* (*fn_t)();
1159 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1160 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1161 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1162 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1163
1164 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1165 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1166 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1167
1168 fn_t ns_get_private_extern_string =
1169 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1170 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1171 fn_t ns_get_private_extern_string_shared =
1172 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1173 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1174
1175 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1176 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1177 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1178
1179 fn_t ns_get_public_extern_string =
1180 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1181 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1182 fn_t ns_get_public_extern_string_shared =
1183 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1184 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1185
1186 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1187 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1188 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1189
1190 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1191 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1192 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1193 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1194 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1195 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1196
1197 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1198 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1199 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1200 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1201 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1202
1203 dlclose(handle1);
1204 dlclose(handle2);
1205}
1206
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001207TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001208 android_set_application_target_sdk_version(42U); // something > 23
1209
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001210 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001211
1212 // preload this library to the default namespace to check if it
1213 // is shared later on.
1214 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001215 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001216 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1217
1218 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001219 android_create_namespace("private_isolated_shared",
1220 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001221 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001222 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001223 nullptr,
1224 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001225 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001226 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001227
1228 // Check if "libnstest_dlopened.so" is loaded (and the same)
1229 android_dlextinfo extinfo;
1230 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1231 extinfo.library_namespace = ns_isolated_shared;
1232
1233 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1234 ASSERT_TRUE(handle != nullptr) << dlerror();
1235 ASSERT_TRUE(handle == handle_dlopened);
1236 dlclose(handle);
1237 dlclose(handle_dlopened);
1238
1239 // And now check that the library cannot be found by soname (and is no longer loaded)
1240 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1241 ASSERT_TRUE(handle == nullptr)
1242 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1243
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001244 handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001245 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1246 ASSERT_TRUE(handle == nullptr)
1247 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1248
1249 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1250 ASSERT_TRUE(handle == nullptr)
1251 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1252
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001253 handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001254 RTLD_NOW | RTLD_NOLOAD);
1255 ASSERT_TRUE(handle == nullptr)
1256 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1257
1258 // Now lets see if the soinfo area gets reused in the wrong way:
1259 // load a library to default namespace.
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001260 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001261 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1262 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1263
1264 // try to find it in shared namespace
1265 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1266 ASSERT_TRUE(handle == nullptr)
1267 << "Error: " << g_public_lib << " is accessible in shared namespace";
1268}
1269
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001270TEST(dlext, ns_isolated_rtld_global) {
1271 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001272 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001273
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001274 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001275
1276 android_namespace_t* ns1 =
1277 android_create_namespace("isolated1",
1278 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001279 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001280 ANDROID_NAMESPACE_TYPE_ISOLATED,
1281 lib_public_path.c_str(),
1282 nullptr);
1283 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001284 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001285
1286 android_namespace_t* ns2 =
1287 android_create_namespace("isolated2",
1288 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001289 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001290 ANDROID_NAMESPACE_TYPE_ISOLATED,
1291 lib_public_path.c_str(),
1292 nullptr);
1293 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001294 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001295
1296 android_dlextinfo extinfo;
1297 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1298 extinfo.library_namespace = ns1;
1299
1300 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1301 RTLD_GLOBAL,
1302 &extinfo);
1303
1304 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1305
1306 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001307 android_create_namespace("isolated1_child",
1308 nullptr,
1309 (get_testlib_root() + "/private_namespace_libs").c_str(),
1310 ANDROID_NAMESPACE_TYPE_ISOLATED,
1311 nullptr,
1312 ns1);
1313
1314 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1315 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001316
1317 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1318 // attempt to use ns2 should result in dlerror()
1319
1320 // Check ns1_child first.
1321 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1322 extinfo.library_namespace = ns1_child;
1323
1324 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1325 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1326
1327 // now ns1
1328 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1329 extinfo.library_namespace = ns1;
1330
1331 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1332 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1333
1334 // and ns2 should fail
1335 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1336 extinfo.library_namespace = ns2;
1337
1338 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1339 ASSERT_TRUE(handle1 == nullptr);
1340 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1341}
1342
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001343TEST(dlext, ns_anonymous) {
1344 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001345 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001346
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001347 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001348 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1349
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001350 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1351
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001352 ASSERT_TRUE(
1353 android_init_anonymous_namespace(shared_libs.c_str(),
1354 (get_testlib_root() + "/private_namespace_libs").c_str())
1355 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001356
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001357 android_namespace_t* ns =
1358 android_create_namespace("private",
1359 nullptr,
1360 (get_testlib_root() + "/private_namespace_libs").c_str(),
1361 ANDROID_NAMESPACE_TYPE_REGULAR,
1362 nullptr,
1363 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001364
1365 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001366 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001367
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001368 std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001369
1370 android_dlextinfo extinfo;
1371 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1372 extinfo.library_namespace = ns;
1373
1374 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1375 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1376 ASSERT_TRUE(handle != nullptr) << dlerror();
1377
1378 uintptr_t ns_get_dlopened_string_addr =
1379 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1380 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1381 typedef const char* (*fn_t)();
1382 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1383
1384 std::vector<map_record> maps;
1385 Maps::parse_maps(&maps);
1386
1387 uintptr_t addr_start = 0;
1388 uintptr_t addr_end = 0;
1389 std::vector<map_record> maps_to_copy;
1390
1391 for (const auto& rec : maps) {
1392 if (rec.pathname == private_library_absolute_path) {
1393 if (addr_start == 0) {
1394 addr_start = rec.addr_start;
1395 }
1396 addr_end = rec.addr_end;
1397
1398 maps_to_copy.push_back(rec);
1399 }
1400 }
1401
1402 // some sanity checks..
1403 ASSERT_TRUE(addr_start > 0);
1404 ASSERT_TRUE(addr_end > 0);
1405 ASSERT_EQ(3U, maps_to_copy.size());
1406 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1407 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1408
1409 // copy
1410 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1411 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1412 -1, 0));
1413 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1414
1415 for (const auto& rec : maps_to_copy) {
1416 uintptr_t offset = rec.addr_start - addr_start;
1417 size_t size = rec.addr_end - rec.addr_start;
1418 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1419 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1420 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1421 ASSERT_TRUE(map != MAP_FAILED);
1422 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1423 mprotect(map, size, rec.perms);
1424 }
1425
1426 // call the function copy
1427 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1428 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1429 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1430 ns_get_dlopened_string_anon());
1431
1432 // They should belong to different namespaces (private and anonymous)
1433 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1434 ns_get_dlopened_string_private());
1435
1436 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1437}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001438
1439TEST(dlext, dlopen_handle_value_platform) {
1440 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1441 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1442 << "dlopen should return odd value for the handle";
1443 dlclose(handle);
1444}
1445
1446TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001447 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001448 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1449 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1450 << "dlopen should return valid pointer";
1451 dlclose(handle);
1452}