blob: 8527b00241be23c418739149452a59ece2b14745 [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 Ivanov927877c2016-09-21 11:17:13 -0700101 const std::string lib_path = g_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 Ivanov927877c2016-09-21 11:17:13 -0700119 const std::string lib_path = g_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 Ivanov927877c2016-09-21 11:17:13 -0700145 const std::string lib_path = g_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";
231 const std::string lib_path = g_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";
245 const std::string lib_path = g_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";
264 const std::string lib_path = g_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
620TEST(dlext, ns_smoke) {
621 static const char* root_lib = "libnstest_root.so";
622 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
623
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800624 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
625 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dimitry Ivanov3a6c6b32016-07-13 16:28:20 -0700626 "a library with soname \"libnstest_public.so\" was not found in the "
627 "default namespace",
628 dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700629
Dimitry Ivanov54807612016-04-21 14:57:38 -0700630 ASSERT_FALSE(android_init_namespaces("", nullptr));
631 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
632 "the list of public libraries is empty.", dlerror());
633
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700634 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800635 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700636 ASSERT_TRUE(handle_public != nullptr) << dlerror();
637
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800638 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700639
640 // Check that libraries added to public namespace are NODELETE
641 dlclose(handle_public);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700642 handle_public = dlopen((g_testlib_root + "/public_namespace_libs/" + g_public_lib).c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800643 RTLD_NOW | RTLD_NOLOAD);
644
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700645 ASSERT_TRUE(handle_public != nullptr) << dlerror();
646
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800647 android_namespace_t* ns1 =
648 android_create_namespace("private", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700649 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700650 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700651 ASSERT_TRUE(ns1 != nullptr) << dlerror();
652
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800653 android_namespace_t* ns2 =
654 android_create_namespace("private_isolated", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700655 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700656 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700657 ASSERT_TRUE(ns2 != nullptr) << dlerror();
658
659 // This should not have affect search path for default namespace:
660 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
661 void* handle = dlopen(g_public_lib, RTLD_NOW);
662 ASSERT_TRUE(handle != nullptr) << dlerror();
663 dlclose(handle);
664
665 android_dlextinfo extinfo;
666 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
667 extinfo.library_namespace = ns1;
668
669 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
670 ASSERT_TRUE(handle1 != nullptr) << dlerror();
671
672 extinfo.library_namespace = ns2;
673 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
674 ASSERT_TRUE(handle2 != nullptr) << dlerror();
675
676 ASSERT_TRUE(handle1 != handle2);
677
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800678 // dlopen for a public library using an absolute path should work for isolated namespaces
679 extinfo.library_namespace = ns2;
680 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
681 ASSERT_TRUE(handle != nullptr) << dlerror();
682 ASSERT_TRUE(handle == handle_public);
683
684 dlclose(handle);
685
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700686 typedef const char* (*fn_t)();
687
688 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
689 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
690 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
691 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
692
693 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
694 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
695
696 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
697
698 fn_t ns_get_private_extern_string1 =
699 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
700 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
701 fn_t ns_get_private_extern_string2 =
702 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
703 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
704
705 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
706 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
707
708 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
709
710 fn_t ns_get_public_extern_string1 =
711 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
712 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
713 fn_t ns_get_public_extern_string2 =
714 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
715 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
716
717 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
718 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
719
720 // and now check that dlopen() does the right thing in terms of preserving namespace
721 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
722 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
723 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
724 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
725
726 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
727 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
728
729 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
730
731 dlclose(handle1);
732
733 // Check if handle2 is still alive (and well)
734 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
735 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
736 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
737 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
738
739 dlclose(handle2);
740}
741
742TEST(dlext, ns_isolated) {
743 static const char* root_lib = "libnstest_root_not_isolated.so";
744 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
745
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700746 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800747 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700748 ASSERT_TRUE(handle_public != nullptr) << dlerror();
749
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800750 android_set_application_target_sdk_version(42U); // something > 23
751
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800752 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700753
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800754 android_namespace_t* ns_not_isolated =
755 android_create_namespace("private", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700756 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700757 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700758 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
759
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800760 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700761 android_create_namespace("private_isolated1",
762 nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700763 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700764 ANDROID_NAMESPACE_TYPE_ISOLATED,
765 nullptr,
766 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700767 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
768
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800769 android_namespace_t* ns_isolated2 =
770 android_create_namespace("private_isolated2",
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700771 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700772 nullptr,
773 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700774 g_testlib_root.c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700775 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700776 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
777
778 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
779 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
780
781 std::string lib_private_external_path =
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700782 g_testlib_root + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700783
784 // Load lib_private_external_path to default namespace
785 // (it should remain invisible for the isolated namespaces after this)
786 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
787 ASSERT_TRUE(handle != nullptr) << dlerror();
788
789 android_dlextinfo extinfo;
790 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
791 extinfo.library_namespace = ns_not_isolated;
792
793 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
794 ASSERT_TRUE(handle1 != nullptr) << dlerror();
795
796 extinfo.library_namespace = ns_isolated;
797
798 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
799 ASSERT_TRUE(handle2 == nullptr);
800 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
801
802 // Check dlopen by absolute path
803 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
804 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800805 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700806 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800807 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700808
809 extinfo.library_namespace = ns_isolated2;
810
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700811 // this should work because isolation_path for private_isolated2 includes g_testlib_root
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700812 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800813 ASSERT_TRUE(handle2 != nullptr) << dlerror();
814 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700815
816 // Check dlopen by absolute path
817 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800818 ASSERT_TRUE(handle2 != nullptr) << dlerror();
819 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700820
821 typedef const char* (*fn_t)();
822 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
823 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
824
825 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
826
827 fn_t ns_get_private_extern_string =
828 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
829 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
830
831 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
832
833 fn_t ns_get_public_extern_string =
834 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
835 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
836
837 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
838
839 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
840 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
841
842 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
843
844 dlclose(handle1);
845}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800846
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800847TEST(dlext, ns_shared) {
848 static const char* root_lib = "libnstest_root_not_isolated.so";
849 static const char* root_lib_isolated = "libnstest_root.so";
850 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
851
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700852 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800853 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
854 ASSERT_TRUE(handle_public != nullptr) << dlerror();
855
856 android_set_application_target_sdk_version(42U); // something > 23
857
858 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
859
860 // preload this library to the default namespace to check if it
861 // is shared later on.
862 void* handle_dlopened =
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700863 dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800864 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
865
866 android_namespace_t* ns_not_isolated =
867 android_create_namespace("private", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700868 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700869 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800870 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
871
872 android_namespace_t* ns_isolated_shared =
873 android_create_namespace("private_isolated_shared", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700874 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800875 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700876 nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800877 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
878
879 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
880 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
881
882 std::string lib_private_external_path =
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700883 g_testlib_root + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800884
885 // Load lib_private_external_path to default namespace
886 // (it should remain invisible for the isolated namespaces after this)
887 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
888 ASSERT_TRUE(handle != nullptr) << dlerror();
889
890 android_dlextinfo extinfo;
891 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
892 extinfo.library_namespace = ns_not_isolated;
893
894 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
895 ASSERT_TRUE(handle1 != nullptr) << dlerror();
896
897 extinfo.library_namespace = ns_isolated_shared;
898
899 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
900 ASSERT_TRUE(handle2 == nullptr);
901 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
902
903 // Check dlopen by absolute path
904 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
905 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800906 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700907 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800908 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800909
910 // load libnstest_root.so to shared namespace in order to check that everything is different
911 // except shared libnstest_dlopened.so
912
913 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
914
915 typedef const char* (*fn_t)();
916 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
917 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
918 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
919 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
920
921 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
922 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
923 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
924
925 fn_t ns_get_private_extern_string =
926 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
927 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
928 fn_t ns_get_private_extern_string_shared =
929 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
930 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
931
932 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
933 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
934 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
935
936 fn_t ns_get_public_extern_string =
937 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
938 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
939 fn_t ns_get_public_extern_string_shared =
940 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
941 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
942
943 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
944 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
945 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
946
947 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
948 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
949 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
950 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
951 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
952 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
953
954 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
955 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
956 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
957 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
958 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
959
960 dlclose(handle1);
961 dlclose(handle2);
962}
963
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700964TEST(dlext, ns_shared_dlclose) {
965 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
966
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700967 android_set_application_target_sdk_version(42U); // something > 23
968
969 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
970
971 // preload this library to the default namespace to check if it
972 // is shared later on.
973 void* handle_dlopened =
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700974 dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700975 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
976
977 android_namespace_t* ns_isolated_shared =
978 android_create_namespace("private_isolated_shared", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700979 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700980 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700981 nullptr, nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700982 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
983
984 // Check if "libnstest_dlopened.so" is loaded (and the same)
985 android_dlextinfo extinfo;
986 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
987 extinfo.library_namespace = ns_isolated_shared;
988
989 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
990 ASSERT_TRUE(handle != nullptr) << dlerror();
991 ASSERT_TRUE(handle == handle_dlopened);
992 dlclose(handle);
993 dlclose(handle_dlopened);
994
995 // And now check that the library cannot be found by soname (and is no longer loaded)
996 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
997 ASSERT_TRUE(handle == nullptr)
998 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
999
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001000 handle = android_dlopen_ext((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001001 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1002 ASSERT_TRUE(handle == nullptr)
1003 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1004
1005 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1006 ASSERT_TRUE(handle == nullptr)
1007 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1008
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001009 handle = dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001010 RTLD_NOW | RTLD_NOLOAD);
1011 ASSERT_TRUE(handle == nullptr)
1012 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1013
1014 // Now lets see if the soinfo area gets reused in the wrong way:
1015 // load a library to default namespace.
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001016 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001017 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1018 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1019
1020 // try to find it in shared namespace
1021 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1022 ASSERT_TRUE(handle == nullptr)
1023 << "Error: " << g_public_lib << " is accessible in shared namespace";
1024}
1025
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001026TEST(dlext, ns_isolated_rtld_global) {
1027 static const char* root_lib = "libnstest_root.so";
1028 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
1029
1030 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
1031
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001032 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001033
1034 android_namespace_t* ns1 =
1035 android_create_namespace("isolated1",
1036 nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001037 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001038 ANDROID_NAMESPACE_TYPE_ISOLATED,
1039 lib_public_path.c_str(),
1040 nullptr);
1041 ASSERT_TRUE(ns1 != nullptr) << dlerror();
1042
1043 android_namespace_t* ns2 =
1044 android_create_namespace("isolated2",
1045 nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001046 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001047 ANDROID_NAMESPACE_TYPE_ISOLATED,
1048 lib_public_path.c_str(),
1049 nullptr);
1050 ASSERT_TRUE(ns2 != nullptr) << dlerror();
1051
1052 android_dlextinfo extinfo;
1053 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1054 extinfo.library_namespace = ns1;
1055
1056 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1057 RTLD_GLOBAL,
1058 &extinfo);
1059
1060 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1061
1062 android_namespace_t* ns1_child =
1063 android_create_namespace("isolated1_child",
1064 nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001065 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001066 ANDROID_NAMESPACE_TYPE_ISOLATED,
1067 nullptr,
1068 ns1);
1069
1070 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1071 // attempt to use ns2 should result in dlerror()
1072
1073 // Check ns1_child first.
1074 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1075 extinfo.library_namespace = ns1_child;
1076
1077 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1078 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1079
1080 // now ns1
1081 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1082 extinfo.library_namespace = ns1;
1083
1084 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1085 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1086
1087 // and ns2 should fail
1088 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1089 extinfo.library_namespace = ns2;
1090
1091 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1092 ASSERT_TRUE(handle1 == nullptr);
1093 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1094}
1095
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001096TEST(dlext, ns_anonymous) {
1097 static const char* root_lib = "libnstest_root.so";
1098 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
1099
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001100 const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001101 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1102
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001103 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1104
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001105 ASSERT_TRUE(android_init_namespaces(path.c_str(), (g_testlib_root + "/private_namespace_libs").c_str()))
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001106 << dlerror();
1107
1108 android_namespace_t* ns = android_create_namespace(
1109 "private", nullptr,
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001110 (g_testlib_root + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001111 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001112
1113 ASSERT_TRUE(ns != nullptr) << dlerror();
1114
Dimitry Ivanov927877c2016-09-21 11:17:13 -07001115 std::string private_library_absolute_path = g_testlib_root + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001116
1117 android_dlextinfo extinfo;
1118 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1119 extinfo.library_namespace = ns;
1120
1121 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1122 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1123 ASSERT_TRUE(handle != nullptr) << dlerror();
1124
1125 uintptr_t ns_get_dlopened_string_addr =
1126 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1127 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1128 typedef const char* (*fn_t)();
1129 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1130
1131 std::vector<map_record> maps;
1132 Maps::parse_maps(&maps);
1133
1134 uintptr_t addr_start = 0;
1135 uintptr_t addr_end = 0;
1136 std::vector<map_record> maps_to_copy;
1137
1138 for (const auto& rec : maps) {
1139 if (rec.pathname == private_library_absolute_path) {
1140 if (addr_start == 0) {
1141 addr_start = rec.addr_start;
1142 }
1143 addr_end = rec.addr_end;
1144
1145 maps_to_copy.push_back(rec);
1146 }
1147 }
1148
1149 // some sanity checks..
1150 ASSERT_TRUE(addr_start > 0);
1151 ASSERT_TRUE(addr_end > 0);
1152 ASSERT_EQ(3U, maps_to_copy.size());
1153 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1154 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1155
1156 // copy
1157 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1158 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1159 -1, 0));
1160 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1161
1162 for (const auto& rec : maps_to_copy) {
1163 uintptr_t offset = rec.addr_start - addr_start;
1164 size_t size = rec.addr_end - rec.addr_start;
1165 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1166 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1167 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1168 ASSERT_TRUE(map != MAP_FAILED);
1169 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1170 mprotect(map, size, rec.perms);
1171 }
1172
1173 // call the function copy
1174 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1175 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1176 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1177 ns_get_dlopened_string_anon());
1178
1179 // They should belong to different namespaces (private and anonymous)
1180 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1181 ns_get_dlopened_string_private());
1182
1183 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1184}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001185
1186TEST(dlext, dlopen_handle_value_platform) {
1187 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1188 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1189 << "dlopen should return odd value for the handle";
1190 dlclose(handle);
1191}
1192
1193TEST(dlext, dlopen_handle_value_app_compat) {
1194 android_set_application_target_sdk_version(23);
1195 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1196 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1197 << "dlopen should return valid pointer";
1198 dlclose(handle);
1199}
1200