blob: a0226a6f67bcb8d8cc8def51efc45f64c056a84e [file] [log] [blame]
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080020#include <elf.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000021#include <errno.h>
22#include <fcntl.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080023#include <inttypes.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000024#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000027#include <android/dlext.h>
28#include <sys/mman.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010029#include <sys/types.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000030#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000031
Torne (Richard Coles)26052612014-05-02 14:57:42 +010032#include <pagemap/pagemap.h>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080033#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010034
Dimitry Ivanov927877c2016-09-21 11:17:13 -070035#include "gtest_globals.h"
Yabin Cui294d1e22014-12-07 20:43:37 -080036#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080037#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070038#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070039#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000040
41#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070042 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000043
44#define ASSERT_DL_ZERO(i) \
45 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
46
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000047#define ASSERT_NOERROR(i) \
48 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
49
Yabin Cui16f7f8d2014-11-04 11:08:05 -080050#define ASSERT_SUBSTR(needle, haystack) \
51 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
52
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053
54typedef int (*fn)(void);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070055constexpr const char* kLibName = "libdlext_test.so";
56constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
57constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
58constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070059
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000060class DlExtTest : public ::testing::Test {
61protected:
62 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070063 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000064 // verify that we don't have the library loaded already
Dimitry Ivanov927877c2016-09-21 11:17:13 -070065 void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070066 ASSERT_TRUE(h == nullptr);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070067 h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070068 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000069 // call dlerror() to swallow the error, and check it was the one we wanted
Dimitry Ivanov927877c2016-09-21 11:17:13 -070070 ASSERT_EQ(std::string("dlopen failed: library \"") + kLibNameNoRelro + "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000071 }
72
73 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070074 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000075 ASSERT_DL_ZERO(dlclose(handle_));
76 }
77 }
78
79 void* handle_;
80};
81
82TEST_F(DlExtTest, ExtInfoNull) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -070083 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000084 ASSERT_DL_NOTNULL(handle_);
85 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
86 ASSERT_DL_NOTNULL(f);
87 EXPECT_EQ(4, f());
88}
89
90TEST_F(DlExtTest, ExtInfoNoFlags) {
91 android_dlextinfo extinfo;
92 extinfo.flags = 0;
Dimitry Ivanov927877c2016-09-21 11:17:13 -070093 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000094 ASSERT_DL_NOTNULL(handle_);
95 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
96 ASSERT_DL_NOTNULL(f);
97 EXPECT_EQ(4, f());
98}
99
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700100TEST_F(DlExtTest, ExtInfoUseFd) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800101 const std::string lib_path = get_testlib_root() + "/libdlext_test_fd/libdlext_test_fd.so";
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700102
103 android_dlextinfo extinfo;
104 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700105 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700106 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700107 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108 ASSERT_DL_NOTNULL(handle_);
109 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
110 ASSERT_DL_NOTNULL(f);
111 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700112
113 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
114 ASSERT_DL_NOTNULL(taxicab_number);
115 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700116}
117
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700118TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800119 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700120
121 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700122 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700123 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800124
125 // Find the offset of the shared library in the zip.
126 ZipArchiveHandle handle;
127 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
128 ZipEntry zip_entry;
129 ZipString zip_name;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700130 zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
131 zip_name.name_length = strlen(kLibZipSimpleZip);
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800132 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
133 extinfo.library_fd_offset = zip_entry.offset;
134 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700135
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700136 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700137 ASSERT_DL_NOTNULL(handle_);
138
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700139 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
140 ASSERT_DL_NOTNULL(taxicab_number);
141 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700142}
143
144TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800145 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700146
147 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700148 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700149 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700150 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700151
152 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
153 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700154 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
155
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800156 // Test an address above 2^44, for http://b/18178121 .
157 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700158 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800160 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
161
162 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
163 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
164 ASSERT_TRUE(handle_ == nullptr);
165 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
166
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700167 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700168 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800169 ASSERT_TRUE(handle_ == nullptr);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700170 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800172 // Check if dlsym works after unsuccessful dlopen().
173 // Supply non-exiting one to make linker visit every soinfo.
174 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
175 ASSERT_TRUE(sym == nullptr);
176
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700177 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700178}
179
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800180TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700181 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800183 // This offset will not be used, so it doesn't matter.
184 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700185
186 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
187 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700188 ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror());
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700189}
190
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700191TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700192 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
193 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700194 // 1. Open actual file
195 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
196 ASSERT_DL_NOTNULL(handle);
197 // 2. Open link with force_load flag set
198 android_dlextinfo extinfo;
199 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700200 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700201 ASSERT_DL_NOTNULL(handle2);
202 ASSERT_TRUE(handle != handle2);
203
204 dlclose(handle2);
205 dlclose(handle);
206}
207
208TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700209 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
210 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700211 // Check if soname lookup still returns already loaded library
212 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700213 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700214 ASSERT_DL_NOTNULL(handle);
215
216 android_dlextinfo extinfo;
217 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
218
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700219 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700220 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
221
222 ASSERT_DL_NOTNULL(handle2);
223 ASSERT_TRUE(handle == handle2);
224
225 dlclose(handle2);
226 dlclose(handle);
227}
228
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700229TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700230 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800231 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700232
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700233 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700234 ASSERT_TRUE(handle != nullptr) << dlerror();
235
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700236 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
237 ASSERT_DL_NOTNULL(taxicab_number);
238 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700239
240 dlclose(handle);
241}
242
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700243TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700244 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800245 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700246
247 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
248
249 ASSERT_TRUE(handle != nullptr) << dlerror();
250
251 typedef void *(* dlopen_b_fn)();
252 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
253 ASSERT_TRUE(fn != nullptr) << dlerror();
254
255 void *p = fn();
256 ASSERT_TRUE(p != nullptr) << dlerror();
257
258 dlclose(p);
259 dlclose(handle);
260}
261
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700262TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700263 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800264 const std::string lib_path = get_testlib_root() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700265
266 typedef void (*fn_t)(const char*);
267 fn_t android_update_LD_LIBRARY_PATH =
268 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
269
270 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
271
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700272 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700273 ASSERT_TRUE(handle == nullptr);
274
275 android_update_LD_LIBRARY_PATH(lib_path.c_str());
276
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700277 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700278 ASSERT_TRUE(handle != nullptr) << dlerror();
279
280 int (*fn)(void);
281 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
282 ASSERT_TRUE(fn != nullptr);
283 EXPECT_EQ(4, fn());
284
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800285 uint32_t* taxicab_number =
286 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700287 ASSERT_DL_NOTNULL(taxicab_number);
288 EXPECT_EQ(1729U, *taxicab_number);
289
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700290 dlclose(handle);
291}
292
293
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000294TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700295 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000296 ASSERT_TRUE(start != MAP_FAILED);
297 android_dlextinfo extinfo;
298 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
299 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700300 extinfo.reserved_size = kLibSize;
301 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000302 ASSERT_DL_NOTNULL(handle_);
303 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
304 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700305 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000306 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700307 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000308 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800309
310 // Check that after dlclose reserved address space is unmapped (and can be reused)
311 dlclose(handle_);
312 handle_ = nullptr;
313
314 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
315 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000316}
317
318TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800319 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000320 ASSERT_TRUE(start != MAP_FAILED);
321 android_dlextinfo extinfo;
322 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
323 extinfo.reserved_addr = start;
324 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700325 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700326 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000327}
328
329TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700330 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000331 ASSERT_TRUE(start != MAP_FAILED);
332 android_dlextinfo extinfo;
333 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
334 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700335 extinfo.reserved_size = kLibSize;
336 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000337 ASSERT_DL_NOTNULL(handle_);
338 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
339 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700340 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000341 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700342 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000343 EXPECT_EQ(4, f());
344}
345
346TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800347 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000348 ASSERT_TRUE(start != MAP_FAILED);
349 android_dlextinfo extinfo;
350 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
351 extinfo.reserved_addr = start;
352 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700353 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000354 ASSERT_DL_NOTNULL(handle_);
355 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
356 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700357 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
358 (reinterpret_cast<void*>(f) >=
359 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000360 EXPECT_EQ(4, f());
361}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000362
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700363TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700364 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700365 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700366 munmap(start, kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700367
368 android_dlextinfo extinfo;
369 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
370 extinfo.reserved_addr = start;
371
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700372 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700373 ASSERT_DL_NOTNULL(handle_);
374 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
375 ASSERT_DL_NOTNULL(f);
376 EXPECT_GE(reinterpret_cast<void*>(f), start);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700377 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700378
379 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800380 dlclose(handle_);
381 handle_ = nullptr;
382
383 // Check that dlclose unmapped the file
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700384 void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800385 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700386}
387
388TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700389 void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700390 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
391 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700392 munmap(start, kLibSize + PAGE_SIZE);
393 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700394 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
395 ASSERT_TRUE(new_addr != MAP_FAILED);
396
397 android_dlextinfo extinfo;
398 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
399 extinfo.reserved_addr = start;
400
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700401 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700402 ASSERT_TRUE(handle_ == nullptr);
403}
404
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100405class DlExtRelroSharingTest : public DlExtTest {
406protected:
407 virtual void SetUp() {
408 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700409 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410 ASSERT_TRUE(start != MAP_FAILED);
411 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
412 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700413 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100414 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000415 }
416
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100417 virtual void TearDown() {
418 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100419 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000420
Yabin Cui294d1e22014-12-07 20:43:37 -0800421 void CreateRelroFile(const char* lib, const char* relro_file) {
422 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100423 ASSERT_NOERROR(relro_fd);
424
425 pid_t pid = fork();
426 if (pid == 0) {
427 // child process
428 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
429 extinfo_.relro_fd = relro_fd;
430 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700431 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100432 fprintf(stderr, "in child: %s\n", dlerror());
433 exit(1);
434 }
435 exit(0);
436 }
437
438 // continuing in parent
439 ASSERT_NOERROR(close(relro_fd));
440 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800441 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100442
443 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800444 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100445 ASSERT_NOERROR(relro_fd);
446 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
447 extinfo_.relro_fd = relro_fd;
448 }
449
450 void TryUsingRelro(const char* lib) {
451 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
452 ASSERT_DL_NOTNULL(handle_);
453 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
454 ASSERT_DL_NOTNULL(f);
455 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700456
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800457 uint32_t* taxicab_number =
458 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700459 ASSERT_DL_NOTNULL(taxicab_number);
460 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100461 }
462
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100463 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
464
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100465 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100466};
467
468TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800469 TemporaryFile tf; // Use tf to get an unique filename.
470 ASSERT_NOERROR(close(tf.fd));
471
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700472 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
473 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Yabin Cui294d1e22014-12-07 20:43:37 -0800474
475 // Use destructor of tf to close and unlink the file.
476 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100477}
478
479TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800480 TemporaryFile tf; // // Use tf to get an unique filename.
481 ASSERT_NOERROR(close(tf.fd));
482
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700483 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
484 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
Yabin Cui294d1e22014-12-07 20:43:37 -0800485
486 // Use destructor of tf to close and unlink the file.
487 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100488}
489
490TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700491 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000492}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100493
494TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700495 if (geteuid() != 0) {
496 GTEST_LOG_(INFO) << "This test must be run as root.\n";
497 return;
498 }
499
Yabin Cui294d1e22014-12-07 20:43:37 -0800500 TemporaryFile tf; // Use tf to get an unique filename.
501 ASSERT_NOERROR(close(tf.fd));
502
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700503 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
Yabin Cui294d1e22014-12-07 20:43:37 -0800504
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100505 int pipefd[2];
506 ASSERT_NOERROR(pipe(pipefd));
507
508 size_t without_sharing, with_sharing;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700509 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, false, &without_sharing));
510 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, true, &with_sharing));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100511
512 // We expect the sharing to save at least 10% of the total PSS. In practice
513 // it saves 40%+ for this test.
514 size_t expected_size = without_sharing - (without_sharing/10);
515 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800516
517 // Use destructor of tf to close and unlink the file.
518 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100519}
520
521void getPss(pid_t pid, size_t* pss_out) {
522 pm_kernel_t* kernel;
523 ASSERT_EQ(0, pm_kernel_create(&kernel));
524
525 pm_process_t* process;
526 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
527
528 pm_map_t** maps;
529 size_t num_maps;
530 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
531
532 size_t total_pss = 0;
533 for (size_t i = 0; i < num_maps; i++) {
534 pm_memusage_t usage;
535 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
536 total_pss += usage.pss;
537 }
538 *pss_out = total_pss;
539
540 free(maps);
541 pm_process_destroy(process);
542 pm_kernel_destroy(kernel);
543}
544
545void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
546 size_t* pss_out) {
547 const int CHILDREN = 20;
548
549 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800550 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100551 int childpipe[CHILDREN];
552 for (int i=0; i<CHILDREN; ++i) {
553 char read_buf;
554 int child_done_pipe[2], parent_done_pipe[2];
555 ASSERT_NOERROR(pipe(child_done_pipe));
556 ASSERT_NOERROR(pipe(parent_done_pipe));
557
558 pid_t child = fork();
559 if (child == 0) {
560 // close the 'wrong' ends of the pipes in the child
561 close(child_done_pipe[0]);
562 close(parent_done_pipe[1]);
563
564 // open the library
565 void* handle;
566 if (share_relro) {
567 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
568 } else {
569 handle = dlopen(lib, RTLD_NOW);
570 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700571 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100572 fprintf(stderr, "in child: %s\n", dlerror());
573 exit(1);
574 }
575
576 // close write end of child_done_pipe to signal the parent that we're done.
577 close(child_done_pipe[1]);
578
579 // wait for the parent to close parent_done_pipe, then exit
580 read(parent_done_pipe[0], &read_buf, 1);
581 exit(0);
582 }
583
584 ASSERT_NOERROR(child);
585
586 // close the 'wrong' ends of the pipes in the parent
587 close(child_done_pipe[1]);
588 close(parent_done_pipe[0]);
589
590 // wait for the child to be done
591 read(child_done_pipe[0], &read_buf, 1);
592 close(child_done_pipe[0]);
593
594 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800595 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100596 childpipe[i] = parent_done_pipe[1];
597 }
598
599 // Sum the PSS of all the children
600 size_t total_pss = 0;
601 for (int i=0; i<CHILDREN; ++i) {
602 size_t child_pss;
Elliott Hughes33697a02016-01-26 13:04:57 -0800603 ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100604 total_pss += child_pss;
605 }
606 *pss_out = total_pss;
607
608 // Close pipes and wait for children to exit
609 for (int i=0; i<CHILDREN; ++i) {
610 ASSERT_NOERROR(close(childpipe[i]));
611 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800612 for (int i = 0; i < CHILDREN; ++i) {
613 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100614 }
615}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700616
617// Testing namespaces
618static const char* g_public_lib = "libnstest_public.so";
619
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800620// These are libs shared with default namespace
621static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
622
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700623TEST(dlext, ns_smoke) {
624 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800625 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700626
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800627 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
628 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
629 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
630 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700631
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800632 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800633 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700634 ASSERT_TRUE(handle_public != nullptr) << dlerror();
635
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800636 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700637
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800638 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700639 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800640 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800641 ASSERT_TRUE(handle_public == nullptr);
642 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
643 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
644
645 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700646
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800647 // create "public namespace", share limited set of public libraries with
648
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800649 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800650 android_create_namespace("private",
651 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800652 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800653 ANDROID_NAMESPACE_TYPE_REGULAR,
654 nullptr,
655 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700656 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800657 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700658
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800659 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800660 android_create_namespace("private_isolated",
661 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800662 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800663 ANDROID_NAMESPACE_TYPE_ISOLATED,
664 nullptr,
665 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700666 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800667 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700668
669 // This should not have affect search path for default namespace:
670 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
671 void* handle = dlopen(g_public_lib, RTLD_NOW);
672 ASSERT_TRUE(handle != nullptr) << dlerror();
673 dlclose(handle);
674
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700675 // dlopen for a public library using an absolute path should work
676 // 1. For isolated namespaces
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700677 android_dlextinfo extinfo;
678 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700679 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
686 // 1.1 even if it wasn't loaded before
687 dlclose(handle_public);
688
689 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
690 ASSERT_TRUE(handle_public == nullptr);
691 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
692 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
693
694 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
695 ASSERT_TRUE(handle != nullptr) << dlerror();
696
697 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
698 ASSERT_TRUE(handle == handle_public);
699
700 dlclose(handle);
701
702 // 2. And for regular namespaces (make sure it does not load second copy of the library)
703 extinfo.library_namespace = ns1;
704 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
705 ASSERT_TRUE(handle != nullptr) << dlerror();
706 ASSERT_TRUE(handle == handle_public);
707
708 dlclose(handle);
709
710 // 2.1 Unless it was not loaded before - in which case it will load a duplicate.
711 // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces?
712 dlclose(handle_public);
713
714 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
715 ASSERT_TRUE(handle_public == nullptr);
716 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
717 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
718
719 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
720 ASSERT_TRUE(handle != nullptr) << dlerror();
721
722 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
723
724 ASSERT_TRUE(handle != handle_public);
725
726 dlclose(handle);
727
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700728 extinfo.library_namespace = ns1;
729
730 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
731 ASSERT_TRUE(handle1 != nullptr) << dlerror();
732
733 extinfo.library_namespace = ns2;
734 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
735 ASSERT_TRUE(handle2 != nullptr) << dlerror();
736
737 ASSERT_TRUE(handle1 != handle2);
738
739 typedef const char* (*fn_t)();
740
741 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
742 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
743 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
744 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
745
746 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
747 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
748
749 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
750
751 fn_t ns_get_private_extern_string1 =
752 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
753 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
754 fn_t ns_get_private_extern_string2 =
755 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
756 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
757
758 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
759 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
760
761 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
762
763 fn_t ns_get_public_extern_string1 =
764 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
765 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
766 fn_t ns_get_public_extern_string2 =
767 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
768 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
769
770 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
771 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
772
773 // and now check that dlopen() does the right thing in terms of preserving namespace
774 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
775 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
776 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
777 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
778
779 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
780 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
781
782 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
783
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800784 // Check that symbols from non-shared libraries a shared library depends on are not visible
785 // from original namespace.
786
787 fn_t ns_get_internal_extern_string =
788 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
789 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
790 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
791 "ns_get_internal_extern_string() expected to return null but returns \"" <<
792 ns_get_internal_extern_string() << "\"";
793
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700794 dlclose(handle1);
795
796 // Check if handle2 is still alive (and well)
797 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
798 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
799 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
800 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
801
802 dlclose(handle2);
803}
804
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800805TEST(dlext, ns_symbol_visibilty_one_namespace) {
806 static const char* root_lib = "libnstest_root.so";
807 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
808
809 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs:" +
810 get_testlib_root() + "/private_namespace_libs";
811
812 android_namespace_t* ns =
813 android_create_namespace("one",
814 nullptr,
815 ns_search_path.c_str(),
816 ANDROID_NAMESPACE_TYPE_ISOLATED,
817 nullptr,
818 nullptr);
819
820 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
821
822 android_dlextinfo extinfo;
823 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
824 extinfo.library_namespace = ns;
825
826 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
827 ASSERT_TRUE(handle != nullptr) << dlerror();
828
829 typedef const char* (*fn_t)();
830
831 // Check that relocation worked correctly
832 fn_t ns_get_internal_extern_string =
833 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
834 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
835 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
836
837 fn_t internal_extern_string_fn =
838 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
839 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
840 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
841}
842
843TEST(dlext, ns_symbol_visibilty_between_namespaces) {
844 static const char* root_lib = "libnstest_root.so";
845 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
846
847 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
848 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
849
850 android_namespace_t* ns_public =
851 android_create_namespace("public",
852 nullptr,
853 public_ns_search_path.c_str(),
854 ANDROID_NAMESPACE_TYPE_ISOLATED,
855 nullptr,
856 nullptr);
857
858 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
859
860 android_namespace_t* ns_private =
861 android_create_namespace("private",
862 nullptr,
863 private_ns_search_path.c_str(),
864 ANDROID_NAMESPACE_TYPE_ISOLATED,
865 nullptr,
866 nullptr);
867
868 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
869 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
870
871 android_dlextinfo extinfo;
872 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
873 extinfo.library_namespace = ns_private;
874
875 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
876 ASSERT_TRUE(handle != nullptr) << dlerror();
877
878 typedef const char* (*fn_t)();
879
880 // Check that relocation worked correctly
881 fn_t ns_get_internal_extern_string =
882 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
883 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
884 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
885 "ns_get_internal_extern_string() expected to return null but returns \"" <<
886 ns_get_internal_extern_string() << "\"";
887
888 fn_t internal_extern_string_fn =
889 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
890 ASSERT_TRUE(internal_extern_string_fn == nullptr);
891 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
892}
893
894TEST(dlext, ns_unload_between_namespaces) {
895 static const char* root_lib = "libnstest_root.so";
896 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
897
898 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
899 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
900
901 android_namespace_t* ns_public =
902 android_create_namespace("public",
903 nullptr,
904 public_ns_search_path.c_str(),
905 ANDROID_NAMESPACE_TYPE_ISOLATED,
906 nullptr,
907 nullptr);
908
909 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
910
911 android_namespace_t* ns_private =
912 android_create_namespace("private",
913 nullptr,
914 private_ns_search_path.c_str(),
915 ANDROID_NAMESPACE_TYPE_ISOLATED,
916 nullptr,
917 nullptr);
918
919 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
920 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
921
922 android_dlextinfo extinfo;
923 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
924 extinfo.library_namespace = ns_private;
925
926 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
927 ASSERT_TRUE(handle != nullptr) << dlerror();
928
929 dlclose(handle);
930 // Check that root_lib was unloaded
931 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
932 ASSERT_TRUE(handle == nullptr);
933 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
934 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
935
936 // Check that shared library was unloaded in public ns
937 extinfo.library_namespace = ns_public;
938 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
939 ASSERT_TRUE(handle == nullptr);
940 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
941 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
942}
943
Dimitry Ivanov18623142017-02-21 13:41:08 -0800944TEST(dlext, ns_greylist) {
945 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
946
947 const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
948
949 android_namespace_t* ns =
950 android_create_namespace("namespace",
951 nullptr,
952 ns_search_path.c_str(),
953 ANDROID_NAMESPACE_TYPE_ISOLATED,
954 nullptr,
955 nullptr);
956
957 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
958
959 android_dlextinfo extinfo;
960 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
961 extinfo.library_namespace = ns;
962
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800963 // An app targeting M can open libnativehelper.so because it's on the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -0800964 android_set_application_target_sdk_version(__ANDROID_API_M__);
965 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
966 ASSERT_TRUE(handle != nullptr) << dlerror();
967
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800968 // Check that loader did not load another copy of libdl.so while loading greylisted library.
969 void* dlsym_ptr = dlsym(handle, "dlsym");
970 ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror();
971 ASSERT_EQ(&dlsym, dlsym_ptr);
972
Dimitry Ivanov18623142017-02-21 13:41:08 -0800973 dlclose(handle);
974
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800975 // An app targeting N no longer has the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -0800976 android_set_application_target_sdk_version(__ANDROID_API_N__);
977 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
978 ASSERT_TRUE(handle == nullptr);
979 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
980}
981
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800982TEST(dlext, ns_cyclic_namespaces) {
983 // Test that ns1->ns2->ns1 link does not break the loader
984 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
985 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
986
987 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs";
988
989 android_namespace_t* ns1 =
990 android_create_namespace("ns1",
991 nullptr,
992 ns_search_path.c_str(),
993 ANDROID_NAMESPACE_TYPE_ISOLATED,
994 nullptr,
995 nullptr);
996
997 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
998
999 android_namespace_t* ns2 =
1000 android_create_namespace("ns1",
1001 nullptr,
1002 ns_search_path.c_str(),
1003 ANDROID_NAMESPACE_TYPE_ISOLATED,
1004 nullptr,
1005 nullptr);
1006
1007 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
1008
1009 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
1010 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
1011
1012 android_dlextinfo extinfo;
1013 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1014 extinfo.library_namespace = ns1;
1015
1016 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
1017 ASSERT_TRUE(handle == nullptr);
1018 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
1019}
1020
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001021TEST(dlext, ns_isolated) {
1022 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001023 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001024
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001025 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001026 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001027 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1028
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08001029 android_set_application_target_sdk_version(42U); // something > 23
1030
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001031 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001032
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001033 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001034 android_create_namespace("private",
1035 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001036 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001037 ANDROID_NAMESPACE_TYPE_REGULAR,
1038 nullptr,
1039 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001040 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001041 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001042
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001043 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001044 android_create_namespace("private_isolated1",
1045 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001046 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001047 ANDROID_NAMESPACE_TYPE_ISOLATED,
1048 nullptr,
1049 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001050 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001051 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001052
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001053 android_namespace_t* ns_isolated2 =
1054 android_create_namespace("private_isolated2",
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001055 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001056 nullptr,
1057 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001058 get_testlib_root().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001059 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001060 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001061 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001062
1063 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1064 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1065
1066 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001067 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001068
1069 // Load lib_private_external_path to default namespace
1070 // (it should remain invisible for the isolated namespaces after this)
1071 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1072 ASSERT_TRUE(handle != nullptr) << dlerror();
1073
1074 android_dlextinfo extinfo;
1075 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1076 extinfo.library_namespace = ns_not_isolated;
1077
1078 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1079 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1080
1081 extinfo.library_namespace = ns_isolated;
1082
1083 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1084 ASSERT_TRUE(handle2 == nullptr);
1085 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1086
1087 // Check dlopen by absolute path
1088 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1089 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001090 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001091 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001092 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001093
1094 extinfo.library_namespace = ns_isolated2;
1095
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001096 // this should work because isolation_path for private_isolated2 includes get_testlib_root()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001097 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001098 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1099 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001100
1101 // Check dlopen by absolute path
1102 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001103 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1104 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001105
1106 typedef const char* (*fn_t)();
1107 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1108 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1109
1110 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1111
1112 fn_t ns_get_private_extern_string =
1113 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1114 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1115
1116 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1117
1118 fn_t ns_get_public_extern_string =
1119 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1120 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1121
1122 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1123
1124 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1125 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1126
1127 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1128
1129 dlclose(handle1);
1130}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001131
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001132TEST(dlext, ns_shared) {
1133 static const char* root_lib = "libnstest_root_not_isolated.so";
1134 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001135
1136 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001137
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001138 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001139 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1140 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1141
1142 android_set_application_target_sdk_version(42U); // something > 23
1143
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001144 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001145
1146 // preload this library to the default namespace to check if it
1147 // is shared later on.
1148 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001149 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001150 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1151
1152 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001153 android_create_namespace("private",
1154 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001155 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001156 ANDROID_NAMESPACE_TYPE_REGULAR,
1157 nullptr,
1158 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001159 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001160 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001161
1162 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001163 android_create_namespace("private_isolated_shared",
1164 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001165 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001166 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001167 nullptr,
1168 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001169 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001170 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001171
1172 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1173 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1174
1175 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001176 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001177
1178 // Load lib_private_external_path to default namespace
1179 // (it should remain invisible for the isolated namespaces after this)
1180 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1181 ASSERT_TRUE(handle != nullptr) << dlerror();
1182
1183 android_dlextinfo extinfo;
1184 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1185 extinfo.library_namespace = ns_not_isolated;
1186
1187 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1188 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1189
1190 extinfo.library_namespace = ns_isolated_shared;
1191
1192 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1193 ASSERT_TRUE(handle2 == nullptr);
1194 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1195
1196 // Check dlopen by absolute path
1197 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1198 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001199 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001200 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001201 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001202
1203 // load libnstest_root.so to shared namespace in order to check that everything is different
1204 // except shared libnstest_dlopened.so
1205
1206 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1207
1208 typedef const char* (*fn_t)();
1209 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1210 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1211 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1212 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1213
1214 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1215 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1216 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1217
1218 fn_t ns_get_private_extern_string =
1219 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1220 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1221 fn_t ns_get_private_extern_string_shared =
1222 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1223 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1224
1225 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1226 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1227 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1228
1229 fn_t ns_get_public_extern_string =
1230 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1231 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1232 fn_t ns_get_public_extern_string_shared =
1233 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1234 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1235
1236 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1237 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1238 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1239
1240 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1241 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1242 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1243 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1244 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1245 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1246
1247 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1248 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1249 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1250 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1251 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1252
1253 dlclose(handle1);
1254 dlclose(handle2);
1255}
1256
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001257TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001258 android_set_application_target_sdk_version(42U); // something > 23
1259
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001260 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001261
1262 // preload this library to the default namespace to check if it
1263 // is shared later on.
1264 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001265 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001266 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1267
1268 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001269 android_create_namespace("private_isolated_shared",
1270 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001271 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001272 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001273 nullptr,
1274 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001275 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001276 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001277
1278 // Check if "libnstest_dlopened.so" is loaded (and the same)
1279 android_dlextinfo extinfo;
1280 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1281 extinfo.library_namespace = ns_isolated_shared;
1282
1283 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1284 ASSERT_TRUE(handle != nullptr) << dlerror();
1285 ASSERT_TRUE(handle == handle_dlopened);
1286 dlclose(handle);
1287 dlclose(handle_dlopened);
1288
1289 // And now check that the library cannot be found by soname (and is no longer loaded)
1290 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1291 ASSERT_TRUE(handle == nullptr)
1292 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1293
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001294 handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001295 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1296 ASSERT_TRUE(handle == nullptr)
1297 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1298
1299 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1300 ASSERT_TRUE(handle == nullptr)
1301 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1302
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001303 handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001304 RTLD_NOW | RTLD_NOLOAD);
1305 ASSERT_TRUE(handle == nullptr)
1306 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1307
1308 // Now lets see if the soinfo area gets reused in the wrong way:
1309 // load a library to default namespace.
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001310 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001311 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1312 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1313
1314 // try to find it in shared namespace
1315 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1316 ASSERT_TRUE(handle == nullptr)
1317 << "Error: " << g_public_lib << " is accessible in shared namespace";
1318}
1319
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001320TEST(dlext, ns_isolated_rtld_global) {
1321 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001322 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001323
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001324 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001325
1326 android_namespace_t* ns1 =
1327 android_create_namespace("isolated1",
1328 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001329 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001330 ANDROID_NAMESPACE_TYPE_ISOLATED,
1331 lib_public_path.c_str(),
1332 nullptr);
1333 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001334 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001335
1336 android_namespace_t* ns2 =
1337 android_create_namespace("isolated2",
1338 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001339 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001340 ANDROID_NAMESPACE_TYPE_ISOLATED,
1341 lib_public_path.c_str(),
1342 nullptr);
1343 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001344 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001345
1346 android_dlextinfo extinfo;
1347 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1348 extinfo.library_namespace = ns1;
1349
1350 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1351 RTLD_GLOBAL,
1352 &extinfo);
1353
1354 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1355
1356 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001357 android_create_namespace("isolated1_child",
1358 nullptr,
1359 (get_testlib_root() + "/private_namespace_libs").c_str(),
1360 ANDROID_NAMESPACE_TYPE_ISOLATED,
1361 nullptr,
1362 ns1);
1363
1364 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1365 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001366
1367 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1368 // attempt to use ns2 should result in dlerror()
1369
1370 // Check ns1_child first.
1371 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1372 extinfo.library_namespace = ns1_child;
1373
1374 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1375 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1376
1377 // now ns1
1378 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1379 extinfo.library_namespace = ns1;
1380
1381 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1382 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1383
1384 // and ns2 should fail
1385 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1386 extinfo.library_namespace = ns2;
1387
1388 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1389 ASSERT_TRUE(handle1 == nullptr);
1390 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1391}
1392
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001393TEST(dlext, ns_anonymous) {
1394 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001395 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001396
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001397 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001398 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1399
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001400 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1401
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001402 ASSERT_TRUE(
1403 android_init_anonymous_namespace(shared_libs.c_str(),
1404 (get_testlib_root() + "/private_namespace_libs").c_str())
1405 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001406
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001407 android_namespace_t* ns =
1408 android_create_namespace("private",
1409 nullptr,
1410 (get_testlib_root() + "/private_namespace_libs").c_str(),
1411 ANDROID_NAMESPACE_TYPE_REGULAR,
1412 nullptr,
1413 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001414
1415 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001416 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001417
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001418 std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001419
1420 android_dlextinfo extinfo;
1421 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1422 extinfo.library_namespace = ns;
1423
1424 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1425 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1426 ASSERT_TRUE(handle != nullptr) << dlerror();
1427
1428 uintptr_t ns_get_dlopened_string_addr =
1429 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1430 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1431 typedef const char* (*fn_t)();
1432 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1433
1434 std::vector<map_record> maps;
1435 Maps::parse_maps(&maps);
1436
1437 uintptr_t addr_start = 0;
1438 uintptr_t addr_end = 0;
1439 std::vector<map_record> maps_to_copy;
1440
1441 for (const auto& rec : maps) {
1442 if (rec.pathname == private_library_absolute_path) {
1443 if (addr_start == 0) {
1444 addr_start = rec.addr_start;
1445 }
1446 addr_end = rec.addr_end;
1447
1448 maps_to_copy.push_back(rec);
1449 }
1450 }
1451
1452 // some sanity checks..
1453 ASSERT_TRUE(addr_start > 0);
1454 ASSERT_TRUE(addr_end > 0);
1455 ASSERT_EQ(3U, maps_to_copy.size());
1456 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1457 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1458
1459 // copy
1460 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1461 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1462 -1, 0));
1463 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1464
1465 for (const auto& rec : maps_to_copy) {
1466 uintptr_t offset = rec.addr_start - addr_start;
1467 size_t size = rec.addr_end - rec.addr_start;
1468 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1469 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1470 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1471 ASSERT_TRUE(map != MAP_FAILED);
1472 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1473 mprotect(map, size, rec.perms);
1474 }
1475
1476 // call the function copy
1477 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1478 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1479 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1480 ns_get_dlopened_string_anon());
1481
1482 // They should belong to different namespaces (private and anonymous)
1483 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1484 ns_get_dlopened_string_private());
1485
1486 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1487}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001488
1489TEST(dlext, dlopen_handle_value_platform) {
1490 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1491 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1492 << "dlopen should return odd value for the handle";
1493 dlclose(handle);
1494}
1495
1496TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001497 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001498 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1499 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1500 << "dlopen should return valid pointer";
1501 dlclose(handle);
1502}