blob: fdb73655d81ff1b09421b433b55d1d922f048ff3 [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
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
Dimitry Ivanov54807612016-04-21 14:57:38 -0700624 ASSERT_FALSE(android_init_namespaces("", nullptr));
625 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
626 "the list of public libraries is empty.", dlerror());
627
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800628 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800629 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700630 ASSERT_TRUE(handle_public != nullptr) << dlerror();
631
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800632 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700633
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800634 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700635 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800636 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800637
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800638 ASSERT_TRUE(handle_public == nullptr);
639 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
640 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
641
642 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700643
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800644 android_namespace_t* ns1 =
645 android_create_namespace("private", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800646 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700647 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700648 ASSERT_TRUE(ns1 != nullptr) << dlerror();
649
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800650 android_namespace_t* ns2 =
651 android_create_namespace("private_isolated", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800652 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700653 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700654 ASSERT_TRUE(ns2 != nullptr) << dlerror();
655
656 // This should not have affect search path for default namespace:
657 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
658 void* handle = dlopen(g_public_lib, RTLD_NOW);
659 ASSERT_TRUE(handle != nullptr) << dlerror();
660 dlclose(handle);
661
662 android_dlextinfo extinfo;
663 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
664 extinfo.library_namespace = ns1;
665
666 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
667 ASSERT_TRUE(handle1 != nullptr) << dlerror();
668
669 extinfo.library_namespace = ns2;
670 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
671 ASSERT_TRUE(handle2 != nullptr) << dlerror();
672
673 ASSERT_TRUE(handle1 != handle2);
674
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800675 // dlopen for a public library using an absolute path should work for isolated namespaces
676 extinfo.library_namespace = ns2;
677 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
678 ASSERT_TRUE(handle != nullptr) << dlerror();
679 ASSERT_TRUE(handle == handle_public);
680
681 dlclose(handle);
682
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700683 typedef const char* (*fn_t)();
684
685 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
686 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
687 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
688 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
689
690 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
691 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
692
693 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
694
695 fn_t ns_get_private_extern_string1 =
696 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
697 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
698 fn_t ns_get_private_extern_string2 =
699 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
700 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
701
702 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
703 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
704
705 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
706
707 fn_t ns_get_public_extern_string1 =
708 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
709 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
710 fn_t ns_get_public_extern_string2 =
711 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
712 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
713
714 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
715 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
716
717 // and now check that dlopen() does the right thing in terms of preserving namespace
718 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
719 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
720 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
721 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
722
723 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
724 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
725
726 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
727
728 dlclose(handle1);
729
730 // Check if handle2 is still alive (and well)
731 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
732 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
733 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
734 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
735
736 dlclose(handle2);
737}
738
739TEST(dlext, ns_isolated) {
740 static const char* root_lib = "libnstest_root_not_isolated.so";
741 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
742
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800743 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800744 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700745 ASSERT_TRUE(handle_public != nullptr) << dlerror();
746
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800747 android_set_application_target_sdk_version(42U); // something > 23
748
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800749 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700750
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800751 android_namespace_t* ns_not_isolated =
752 android_create_namespace("private", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800753 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700754 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700755 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
756
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800757 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700758 android_create_namespace("private_isolated1",
759 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800760 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700761 ANDROID_NAMESPACE_TYPE_ISOLATED,
762 nullptr,
763 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700764 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
765
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800766 android_namespace_t* ns_isolated2 =
767 android_create_namespace("private_isolated2",
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800768 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700769 nullptr,
770 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800771 get_testlib_root().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700772 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700773 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
774
775 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
776 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
777
778 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800779 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700780
781 // Load lib_private_external_path to default namespace
782 // (it should remain invisible for the isolated namespaces after this)
783 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
784 ASSERT_TRUE(handle != nullptr) << dlerror();
785
786 android_dlextinfo extinfo;
787 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
788 extinfo.library_namespace = ns_not_isolated;
789
790 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
791 ASSERT_TRUE(handle1 != nullptr) << dlerror();
792
793 extinfo.library_namespace = ns_isolated;
794
795 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
796 ASSERT_TRUE(handle2 == nullptr);
797 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
798
799 // Check dlopen by absolute path
800 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
801 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800802 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700803 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800804 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700805
806 extinfo.library_namespace = ns_isolated2;
807
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800808 // this should work because isolation_path for private_isolated2 includes get_testlib_root()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700809 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800810 ASSERT_TRUE(handle2 != nullptr) << dlerror();
811 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700812
813 // Check dlopen by absolute path
814 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800815 ASSERT_TRUE(handle2 != nullptr) << dlerror();
816 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700817
818 typedef const char* (*fn_t)();
819 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
820 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
821
822 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
823
824 fn_t ns_get_private_extern_string =
825 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
826 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
827
828 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
829
830 fn_t ns_get_public_extern_string =
831 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
832 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
833
834 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
835
836 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
837 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
838
839 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
840
841 dlclose(handle1);
842}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800843
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800844TEST(dlext, ns_shared) {
845 static const char* root_lib = "libnstest_root_not_isolated.so";
846 static const char* root_lib_isolated = "libnstest_root.so";
847 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
848
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800849 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800850 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
851 ASSERT_TRUE(handle_public != nullptr) << dlerror();
852
853 android_set_application_target_sdk_version(42U); // something > 23
854
855 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
856
857 // preload this library to the default namespace to check if it
858 // is shared later on.
859 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800860 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800861 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
862
863 android_namespace_t* ns_not_isolated =
864 android_create_namespace("private", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800865 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700866 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800867 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
868
869 android_namespace_t* ns_isolated_shared =
870 android_create_namespace("private_isolated_shared", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800871 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800872 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700873 nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800874 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
875
876 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
877 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
878
879 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800880 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800881
882 // Load lib_private_external_path to default namespace
883 // (it should remain invisible for the isolated namespaces after this)
884 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
885 ASSERT_TRUE(handle != nullptr) << dlerror();
886
887 android_dlextinfo extinfo;
888 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
889 extinfo.library_namespace = ns_not_isolated;
890
891 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
892 ASSERT_TRUE(handle1 != nullptr) << dlerror();
893
894 extinfo.library_namespace = ns_isolated_shared;
895
896 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
897 ASSERT_TRUE(handle2 == nullptr);
898 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
899
900 // Check dlopen by absolute path
901 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
902 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800903 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700904 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800905 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800906
907 // load libnstest_root.so to shared namespace in order to check that everything is different
908 // except shared libnstest_dlopened.so
909
910 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
911
912 typedef const char* (*fn_t)();
913 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
914 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
915 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
916 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
917
918 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
919 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
920 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
921
922 fn_t ns_get_private_extern_string =
923 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
924 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
925 fn_t ns_get_private_extern_string_shared =
926 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
927 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
928
929 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
930 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
931 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
932
933 fn_t ns_get_public_extern_string =
934 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
935 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
936 fn_t ns_get_public_extern_string_shared =
937 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
938 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
939
940 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
941 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
942 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
943
944 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
945 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
946 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
947 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
948 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
949 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
950
951 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
952 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
953 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
954 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
955 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
956
957 dlclose(handle1);
958 dlclose(handle2);
959}
960
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700961TEST(dlext, ns_shared_dlclose) {
962 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
963
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700964 android_set_application_target_sdk_version(42U); // something > 23
965
966 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
967
968 // preload this library to the default namespace to check if it
969 // is shared later on.
970 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800971 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700972 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
973
974 android_namespace_t* ns_isolated_shared =
975 android_create_namespace("private_isolated_shared", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800976 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700977 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700978 nullptr, nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700979 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
980
981 // Check if "libnstest_dlopened.so" is loaded (and the same)
982 android_dlextinfo extinfo;
983 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
984 extinfo.library_namespace = ns_isolated_shared;
985
986 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
987 ASSERT_TRUE(handle != nullptr) << dlerror();
988 ASSERT_TRUE(handle == handle_dlopened);
989 dlclose(handle);
990 dlclose(handle_dlopened);
991
992 // And now check that the library cannot be found by soname (and is no longer loaded)
993 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
994 ASSERT_TRUE(handle == nullptr)
995 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
996
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800997 handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700998 RTLD_NOW | RTLD_NOLOAD, &extinfo);
999 ASSERT_TRUE(handle == nullptr)
1000 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1001
1002 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1003 ASSERT_TRUE(handle == nullptr)
1004 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1005
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001006 handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001007 RTLD_NOW | RTLD_NOLOAD);
1008 ASSERT_TRUE(handle == nullptr)
1009 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1010
1011 // Now lets see if the soinfo area gets reused in the wrong way:
1012 // load a library to default namespace.
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001013 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001014 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1015 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1016
1017 // try to find it in shared namespace
1018 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1019 ASSERT_TRUE(handle == nullptr)
1020 << "Error: " << g_public_lib << " is accessible in shared namespace";
1021}
1022
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001023TEST(dlext, ns_isolated_rtld_global) {
1024 static const char* root_lib = "libnstest_root.so";
1025 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
1026
1027 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
1028
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001029 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001030
1031 android_namespace_t* ns1 =
1032 android_create_namespace("isolated1",
1033 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001034 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001035 ANDROID_NAMESPACE_TYPE_ISOLATED,
1036 lib_public_path.c_str(),
1037 nullptr);
1038 ASSERT_TRUE(ns1 != nullptr) << dlerror();
1039
1040 android_namespace_t* ns2 =
1041 android_create_namespace("isolated2",
1042 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001043 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001044 ANDROID_NAMESPACE_TYPE_ISOLATED,
1045 lib_public_path.c_str(),
1046 nullptr);
1047 ASSERT_TRUE(ns2 != nullptr) << dlerror();
1048
1049 android_dlextinfo extinfo;
1050 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1051 extinfo.library_namespace = ns1;
1052
1053 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1054 RTLD_GLOBAL,
1055 &extinfo);
1056
1057 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1058
1059 android_namespace_t* ns1_child =
1060 android_create_namespace("isolated1_child",
1061 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001062 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001063 ANDROID_NAMESPACE_TYPE_ISOLATED,
1064 nullptr,
1065 ns1);
1066
1067 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1068 // attempt to use ns2 should result in dlerror()
1069
1070 // Check ns1_child first.
1071 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1072 extinfo.library_namespace = ns1_child;
1073
1074 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1075 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1076
1077 // now ns1
1078 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1079 extinfo.library_namespace = ns1;
1080
1081 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1082 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1083
1084 // and ns2 should fail
1085 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1086 extinfo.library_namespace = ns2;
1087
1088 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1089 ASSERT_TRUE(handle1 == nullptr);
1090 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1091}
1092
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001093TEST(dlext, ns_anonymous) {
1094 static const char* root_lib = "libnstest_root.so";
1095 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
1096
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001097 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001098 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1099
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001100 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1101
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001102 ASSERT_TRUE(android_init_namespaces(path.c_str(), (get_testlib_root() + "/private_namespace_libs").c_str()))
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001103 << dlerror();
1104
1105 android_namespace_t* ns = android_create_namespace(
1106 "private", nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001107 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001108 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001109
1110 ASSERT_TRUE(ns != nullptr) << dlerror();
1111
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001112 std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001113
1114 android_dlextinfo extinfo;
1115 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1116 extinfo.library_namespace = ns;
1117
1118 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1119 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1120 ASSERT_TRUE(handle != nullptr) << dlerror();
1121
1122 uintptr_t ns_get_dlopened_string_addr =
1123 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1124 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1125 typedef const char* (*fn_t)();
1126 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1127
1128 std::vector<map_record> maps;
1129 Maps::parse_maps(&maps);
1130
1131 uintptr_t addr_start = 0;
1132 uintptr_t addr_end = 0;
1133 std::vector<map_record> maps_to_copy;
1134
1135 for (const auto& rec : maps) {
1136 if (rec.pathname == private_library_absolute_path) {
1137 if (addr_start == 0) {
1138 addr_start = rec.addr_start;
1139 }
1140 addr_end = rec.addr_end;
1141
1142 maps_to_copy.push_back(rec);
1143 }
1144 }
1145
1146 // some sanity checks..
1147 ASSERT_TRUE(addr_start > 0);
1148 ASSERT_TRUE(addr_end > 0);
1149 ASSERT_EQ(3U, maps_to_copy.size());
1150 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1151 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1152
1153 // copy
1154 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1155 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1156 -1, 0));
1157 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1158
1159 for (const auto& rec : maps_to_copy) {
1160 uintptr_t offset = rec.addr_start - addr_start;
1161 size_t size = rec.addr_end - rec.addr_start;
1162 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1163 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1164 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1165 ASSERT_TRUE(map != MAP_FAILED);
1166 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1167 mprotect(map, size, rec.perms);
1168 }
1169
1170 // call the function copy
1171 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1172 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1173 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1174 ns_get_dlopened_string_anon());
1175
1176 // They should belong to different namespaces (private and anonymous)
1177 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1178 ns_get_dlopened_string_private());
1179
1180 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1181}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001182
1183TEST(dlext, dlopen_handle_value_platform) {
1184 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1185 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1186 << "dlopen should return odd value for the handle";
1187 dlclose(handle);
1188}
1189
1190TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001191 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001192 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1193 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1194 << "dlopen should return valid pointer";
1195 dlclose(handle);
1196}