blob: b33cf68fe8da36fee6f989568f2a59ac9068b72e [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>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070027
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000028#include <android/dlext.h>
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070029#include <android-base/file.h>
Zhenhua WANG81aad002017-04-25 11:07:19 +080030#include <android-base/strings.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070031
32#include <linux/memfd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000033#include <sys/mman.h>
Elliott Hughesd7c52622017-06-20 14:26:56 -070034#include <sys/syscall.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010035#include <sys/types.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070036#include <sys/vfs.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000037#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000038
Torne (Richard Coles)26052612014-05-02 14:57:42 +010039#include <pagemap/pagemap.h>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080040#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010041
Dimitry Ivanov927877c2016-09-21 11:17:13 -070042#include "gtest_globals.h"
Yabin Cui294d1e22014-12-07 20:43:37 -080043#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080044#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070045#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070046#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000047
48#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070049 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000050
51#define ASSERT_DL_ZERO(i) \
52 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
53
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000054#define ASSERT_NOERROR(i) \
55 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
56
Yabin Cui16f7f8d2014-11-04 11:08:05 -080057#define ASSERT_SUBSTR(needle, haystack) \
58 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
59
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000060
61typedef int (*fn)(void);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070062constexpr const char* kLibName = "libdlext_test.so";
63constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
64constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
65constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070066
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000067class DlExtTest : public ::testing::Test {
68protected:
69 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070070 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000071 // verify that we don't have the library loaded already
Dimitry Ivanov927877c2016-09-21 11:17:13 -070072 void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070073 ASSERT_TRUE(h == nullptr);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070074 h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070075 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000076 // call dlerror() to swallow the error, and check it was the one we wanted
Dimitry Ivanov927877c2016-09-21 11:17:13 -070077 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 +000078 }
79
80 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070081 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000082 ASSERT_DL_ZERO(dlclose(handle_));
83 }
84 }
85
86 void* handle_;
87};
88
89TEST_F(DlExtTest, ExtInfoNull) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -070090 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000091 ASSERT_DL_NOTNULL(handle_);
92 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
93 ASSERT_DL_NOTNULL(f);
94 EXPECT_EQ(4, f());
95}
96
97TEST_F(DlExtTest, ExtInfoNoFlags) {
98 android_dlextinfo extinfo;
99 extinfo.flags = 0;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700100 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000101 ASSERT_DL_NOTNULL(handle_);
102 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
103 ASSERT_DL_NOTNULL(f);
104 EXPECT_EQ(4, f());
105}
106
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700107TEST_F(DlExtTest, ExtInfoUseFd) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700108 const std::string lib_path = GetTestlibRoot() + "/libdlext_test_fd/libdlext_test_fd.so";
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700109
110 android_dlextinfo extinfo;
111 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700112 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700113 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700114 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700115 ASSERT_DL_NOTNULL(handle_);
116 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
117 ASSERT_DL_NOTNULL(f);
118 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700119
120 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
121 ASSERT_DL_NOTNULL(taxicab_number);
122 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700123}
124
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700125TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700126 const std::string lib_path = GetTestlibRoot() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700127
128 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700129 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700130 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800131
132 // Find the offset of the shared library in the zip.
133 ZipArchiveHandle handle;
134 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
135 ZipEntry zip_entry;
136 ZipString zip_name;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700137 zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
138 zip_name.name_length = strlen(kLibZipSimpleZip);
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800139 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
140 extinfo.library_fd_offset = zip_entry.offset;
141 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700142
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700143 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700144 ASSERT_DL_NOTNULL(handle_);
145
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700146 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
147 ASSERT_DL_NOTNULL(taxicab_number);
148 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700149}
150
151TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700152 const std::string lib_path = GetTestlibRoot() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700153
154 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700155 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700156 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700157 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700158
159 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
160 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700161 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
162
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800163 // Test an address above 2^44, for http://b/18178121 .
164 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700165 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700166 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800167 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
168
169 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
170 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
171 ASSERT_TRUE(handle_ == nullptr);
172 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
173
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700174 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700175 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800176 ASSERT_TRUE(handle_ == nullptr);
Elliott Hughesa8971512018-06-27 14:39:06 -0700177 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic: 504b0304", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700178
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800179 // Check if dlsym works after unsuccessful dlopen().
180 // Supply non-exiting one to make linker visit every soinfo.
181 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
182 ASSERT_TRUE(sym == nullptr);
183
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700184 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700185}
186
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800187TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700188 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700189 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800190 // This offset will not be used, so it doesn't matter.
191 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700192
193 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
194 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700195 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 -0700196}
197
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700198TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700199 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
200 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700201 // 1. Open actual file
202 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
203 ASSERT_DL_NOTNULL(handle);
204 // 2. Open link with force_load flag set
205 android_dlextinfo extinfo;
206 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700207 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700208 ASSERT_DL_NOTNULL(handle2);
209 ASSERT_TRUE(handle != handle2);
210
211 dlclose(handle2);
212 dlclose(handle);
213}
214
215TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700216 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
217 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700218 // Check if soname lookup still returns already loaded library
219 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700220 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700221 ASSERT_DL_NOTNULL(handle);
222
223 android_dlextinfo extinfo;
224 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
225
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700226 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700227 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
228
229 ASSERT_DL_NOTNULL(handle2);
230 ASSERT_TRUE(handle == handle2);
231
232 dlclose(handle2);
233 dlclose(handle);
234}
235
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700236TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700237 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700238 const std::string lib_path = GetTestlibRoot() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700239
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700240 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700241 ASSERT_TRUE(handle != nullptr) << dlerror();
242
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700243 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
244 ASSERT_DL_NOTNULL(taxicab_number);
245 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700246
247 dlclose(handle);
248}
249
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700250TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700251 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700252 const std::string lib_path = GetTestlibRoot() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700253
254 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
255
256 ASSERT_TRUE(handle != nullptr) << dlerror();
257
258 typedef void *(* dlopen_b_fn)();
259 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
260 ASSERT_TRUE(fn != nullptr) << dlerror();
261
262 void *p = fn();
263 ASSERT_TRUE(p != nullptr) << dlerror();
264
265 dlclose(p);
266 dlclose(handle);
267}
268
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700269TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700270 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700271 const std::string lib_path = GetTestlibRoot() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700272
273 typedef void (*fn_t)(const char*);
274 fn_t android_update_LD_LIBRARY_PATH =
275 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
276
277 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
278
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700279 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700280 ASSERT_TRUE(handle == nullptr);
281
282 android_update_LD_LIBRARY_PATH(lib_path.c_str());
283
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700284 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700285 ASSERT_TRUE(handle != nullptr) << dlerror();
286
287 int (*fn)(void);
288 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
289 ASSERT_TRUE(fn != nullptr);
290 EXPECT_EQ(4, fn());
291
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800292 uint32_t* taxicab_number =
293 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700294 ASSERT_DL_NOTNULL(taxicab_number);
295 EXPECT_EQ(1729U, *taxicab_number);
296
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700297 dlclose(handle);
298}
299
300
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000301TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700302 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000303 ASSERT_TRUE(start != MAP_FAILED);
304 android_dlextinfo extinfo;
305 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
306 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700307 extinfo.reserved_size = kLibSize;
308 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000309 ASSERT_DL_NOTNULL(handle_);
310 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
311 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700312 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000313 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700314 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000315 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800316
317 // Check that after dlclose reserved address space is unmapped (and can be reused)
318 dlclose(handle_);
319 handle_ = nullptr;
320
321 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
322 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000323}
324
325TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800326 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000327 ASSERT_TRUE(start != MAP_FAILED);
328 android_dlextinfo extinfo;
329 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
330 extinfo.reserved_addr = start;
331 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700332 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700333 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000334}
335
336TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700337 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000338 ASSERT_TRUE(start != MAP_FAILED);
339 android_dlextinfo extinfo;
340 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
341 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700342 extinfo.reserved_size = kLibSize;
343 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000344 ASSERT_DL_NOTNULL(handle_);
345 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
346 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700347 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000348 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700349 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000350 EXPECT_EQ(4, f());
351}
352
353TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800354 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000355 ASSERT_TRUE(start != MAP_FAILED);
356 android_dlextinfo extinfo;
357 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
358 extinfo.reserved_addr = start;
359 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700360 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000361 ASSERT_DL_NOTNULL(handle_);
362 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
363 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700364 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
365 (reinterpret_cast<void*>(f) >=
366 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000367 EXPECT_EQ(4, f());
368}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000369
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700370TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700371 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700372 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700373 munmap(start, kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700374
375 android_dlextinfo extinfo;
376 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
377 extinfo.reserved_addr = start;
378
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700379 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700380 ASSERT_DL_NOTNULL(handle_);
381 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
382 ASSERT_DL_NOTNULL(f);
383 EXPECT_GE(reinterpret_cast<void*>(f), start);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700384 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700385
386 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800387 dlclose(handle_);
388 handle_ = nullptr;
389
390 // Check that dlclose unmapped the file
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700391 void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800392 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700393}
394
395TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700396 void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700397 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
398 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700399 munmap(start, kLibSize + PAGE_SIZE);
400 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700401 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
402 ASSERT_TRUE(new_addr != MAP_FAILED);
403
404 android_dlextinfo extinfo;
405 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
406 extinfo.reserved_addr = start;
407
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700408 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700409 ASSERT_TRUE(handle_ == nullptr);
410}
411
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100412class DlExtRelroSharingTest : public DlExtTest {
413protected:
414 virtual void SetUp() {
415 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700416 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100417 ASSERT_TRUE(start != MAP_FAILED);
418 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
419 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700420 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100421 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000422 }
423
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100424 virtual void TearDown() {
425 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100426 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000427
Yabin Cui294d1e22014-12-07 20:43:37 -0800428 void CreateRelroFile(const char* lib, const char* relro_file) {
Elliott Hughes5cec3772018-01-19 15:45:23 -0800429 int relro_fd = open(relro_file, O_RDWR | O_TRUNC | O_CLOEXEC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100430 ASSERT_NOERROR(relro_fd);
431
432 pid_t pid = fork();
433 if (pid == 0) {
434 // child process
435 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
436 extinfo_.relro_fd = relro_fd;
437 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700438 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100439 fprintf(stderr, "in child: %s\n", dlerror());
440 exit(1);
441 }
442 exit(0);
443 }
444
445 // continuing in parent
446 ASSERT_NOERROR(close(relro_fd));
447 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800448 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100449
450 // reopen file for reading so it can be used
Elliott Hughes5cec3772018-01-19 15:45:23 -0800451 relro_fd = open(relro_file, O_RDONLY | O_CLOEXEC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100452 ASSERT_NOERROR(relro_fd);
453 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
454 extinfo_.relro_fd = relro_fd;
455 }
456
457 void TryUsingRelro(const char* lib) {
458 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
459 ASSERT_DL_NOTNULL(handle_);
460 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
461 ASSERT_DL_NOTNULL(f);
462 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700463
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800464 uint32_t* taxicab_number =
465 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700466 ASSERT_DL_NOTNULL(taxicab_number);
467 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100468 }
469
Zhenhua WANG81aad002017-04-25 11:07:19 +0800470 void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro,
471 size_t* pss_out);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100472
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100473 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100474};
475
476TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800477 TemporaryFile tf; // Use tf to get an unique filename.
478 ASSERT_NOERROR(close(tf.fd));
479
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700480 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
481 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Yabin Cui294d1e22014-12-07 20:43:37 -0800482
483 // Use destructor of tf to close and unlink the file.
484 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100485}
486
487TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800488 TemporaryFile tf; // // Use tf to get an unique filename.
489 ASSERT_NOERROR(close(tf.fd));
490
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700491 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
492 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
Yabin Cui294d1e22014-12-07 20:43:37 -0800493
494 // Use destructor of tf to close and unlink the file.
495 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100496}
497
498TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700499 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000500}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100501
502TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700503 if (geteuid() != 0) {
504 GTEST_LOG_(INFO) << "This test must be run as root.\n";
505 return;
506 }
507
Yabin Cui294d1e22014-12-07 20:43:37 -0800508 TemporaryFile tf; // Use tf to get an unique filename.
509 ASSERT_NOERROR(close(tf.fd));
510
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700511 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
Yabin Cui294d1e22014-12-07 20:43:37 -0800512
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100513 int pipefd[2];
514 ASSERT_NOERROR(pipe(pipefd));
515
516 size_t without_sharing, with_sharing;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800517 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, false, &without_sharing));
518 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, true, &with_sharing));
519 ASSERT_LT(with_sharing, without_sharing);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100520
Zhenhua WANG81aad002017-04-25 11:07:19 +0800521 // We expect the sharing to save at least 50% of the library's total PSS.
522 // In practice it saves 80%+ for this library in the test.
523 size_t pss_saved = without_sharing - with_sharing;
524 size_t expected_min_saved = without_sharing / 2;
525
526 EXPECT_LT(expected_min_saved, pss_saved);
Yabin Cui294d1e22014-12-07 20:43:37 -0800527
528 // Use destructor of tf to close and unlink the file.
529 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100530}
531
Zhenhua WANG81aad002017-04-25 11:07:19 +0800532void GetPss(bool shared_relro, const char* lib, const char* relro_file, pid_t pid,
533 size_t* total_pss) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100534 pm_kernel_t* kernel;
535 ASSERT_EQ(0, pm_kernel_create(&kernel));
536
537 pm_process_t* process;
538 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
539
540 pm_map_t** maps;
541 size_t num_maps;
542 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
543
Zhenhua WANG81aad002017-04-25 11:07:19 +0800544 // Calculate total PSS of the library.
545 *total_pss = 0;
546 bool saw_relro_file = false;
547 for (size_t i = 0; i < num_maps; ++i) {
548 if (android::base::EndsWith(maps[i]->name, lib) || strcmp(maps[i]->name, relro_file) == 0) {
549 if (strcmp(maps[i]->name, relro_file) == 0) saw_relro_file = true;
550
551 pm_memusage_t usage;
552 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
553 *total_pss += usage.pss;
554 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100555 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100556
557 free(maps);
558 pm_process_destroy(process);
559 pm_kernel_destroy(kernel);
Zhenhua WANG81aad002017-04-25 11:07:19 +0800560
561 if (shared_relro) ASSERT_TRUE(saw_relro_file);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100562}
563
Zhenhua WANG81aad002017-04-25 11:07:19 +0800564void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file,
565 bool share_relro, size_t* pss_out) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100566 const int CHILDREN = 20;
567
568 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800569 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100570 int childpipe[CHILDREN];
571 for (int i=0; i<CHILDREN; ++i) {
572 char read_buf;
573 int child_done_pipe[2], parent_done_pipe[2];
574 ASSERT_NOERROR(pipe(child_done_pipe));
575 ASSERT_NOERROR(pipe(parent_done_pipe));
576
577 pid_t child = fork();
578 if (child == 0) {
579 // close the 'wrong' ends of the pipes in the child
580 close(child_done_pipe[0]);
581 close(parent_done_pipe[1]);
582
583 // open the library
584 void* handle;
585 if (share_relro) {
586 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
587 } else {
588 handle = dlopen(lib, RTLD_NOW);
589 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700590 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100591 fprintf(stderr, "in child: %s\n", dlerror());
592 exit(1);
593 }
594
595 // close write end of child_done_pipe to signal the parent that we're done.
596 close(child_done_pipe[1]);
597
598 // wait for the parent to close parent_done_pipe, then exit
599 read(parent_done_pipe[0], &read_buf, 1);
600 exit(0);
601 }
602
603 ASSERT_NOERROR(child);
604
605 // close the 'wrong' ends of the pipes in the parent
606 close(child_done_pipe[1]);
607 close(parent_done_pipe[0]);
608
609 // wait for the child to be done
610 read(child_done_pipe[0], &read_buf, 1);
611 close(child_done_pipe[0]);
612
613 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800614 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100615 childpipe[i] = parent_done_pipe[1];
616 }
617
Zhenhua WANG81aad002017-04-25 11:07:19 +0800618 // Sum the PSS of tested library of all the children
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100619 size_t total_pss = 0;
620 for (int i=0; i<CHILDREN; ++i) {
621 size_t child_pss;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800622 ASSERT_NO_FATAL_FAILURE(GetPss(share_relro, lib, relro_file, child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100623 total_pss += child_pss;
624 }
625 *pss_out = total_pss;
626
627 // Close pipes and wait for children to exit
628 for (int i=0; i<CHILDREN; ++i) {
629 ASSERT_NOERROR(close(childpipe[i]));
630 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800631 for (int i = 0; i < CHILDREN; ++i) {
632 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100633 }
634}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700635
636// Testing namespaces
637static const char* g_public_lib = "libnstest_public.so";
638
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800639// These are libs shared with default namespace
640static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
641
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700642TEST(dlext, ns_smoke) {
643 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800644 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700645
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800646 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
647 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
648 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
649 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700650
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700651 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800652 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700653 ASSERT_TRUE(handle_public != nullptr) << dlerror();
654
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800655 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700656
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800657 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700658 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800659 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800660 ASSERT_TRUE(handle_public == nullptr);
661 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
662 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
663
664 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700665
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800666 // create "public namespace", share limited set of public libraries with
667
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800668 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800669 android_create_namespace("private",
670 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700671 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800672 ANDROID_NAMESPACE_TYPE_REGULAR,
673 nullptr,
674 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700675 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800676 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700677
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800678 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800679 android_create_namespace("private_isolated",
680 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700681 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800682 ANDROID_NAMESPACE_TYPE_ISOLATED,
683 nullptr,
684 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700685 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800686 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700687
688 // This should not have affect search path for default namespace:
689 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
690 void* handle = dlopen(g_public_lib, RTLD_NOW);
691 ASSERT_TRUE(handle != nullptr) << dlerror();
692 dlclose(handle);
693
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700694 // dlopen for a public library using an absolute path should work
695 // 1. For isolated namespaces
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700696 android_dlextinfo extinfo;
697 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700698 extinfo.library_namespace = ns2;
699 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
700 ASSERT_TRUE(handle != nullptr) << dlerror();
701 ASSERT_TRUE(handle == handle_public);
702
703 dlclose(handle);
704
705 // 1.1 even if it wasn't loaded before
706 dlclose(handle_public);
707
708 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
709 ASSERT_TRUE(handle_public == nullptr);
710 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
711 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
712
713 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
714 ASSERT_TRUE(handle != nullptr) << dlerror();
715
716 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
717 ASSERT_TRUE(handle == handle_public);
718
719 dlclose(handle);
720
721 // 2. And for regular namespaces (make sure it does not load second copy of the library)
722 extinfo.library_namespace = ns1;
723 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
724 ASSERT_TRUE(handle != nullptr) << dlerror();
725 ASSERT_TRUE(handle == handle_public);
726
727 dlclose(handle);
728
729 // 2.1 Unless it was not loaded before - in which case it will load a duplicate.
730 // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces?
731 dlclose(handle_public);
732
733 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
734 ASSERT_TRUE(handle_public == nullptr);
735 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
736 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
737
738 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
739 ASSERT_TRUE(handle != nullptr) << dlerror();
740
741 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
742
743 ASSERT_TRUE(handle != handle_public);
744
745 dlclose(handle);
746
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700747 extinfo.library_namespace = ns1;
748
749 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
750 ASSERT_TRUE(handle1 != nullptr) << dlerror();
751
752 extinfo.library_namespace = ns2;
753 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
754 ASSERT_TRUE(handle2 != nullptr) << dlerror();
755
756 ASSERT_TRUE(handle1 != handle2);
757
758 typedef const char* (*fn_t)();
759
760 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
761 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
762 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
763 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
764
765 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
766 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
767
768 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
769
770 fn_t ns_get_private_extern_string1 =
771 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
772 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
773 fn_t ns_get_private_extern_string2 =
774 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
775 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
776
777 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
778 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
779
780 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
781
782 fn_t ns_get_public_extern_string1 =
783 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
784 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
785 fn_t ns_get_public_extern_string2 =
786 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
787 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
788
789 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
790 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
791
792 // and now check that dlopen() does the right thing in terms of preserving namespace
793 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
794 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
795 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
796 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
797
798 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
799 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
800
801 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
802
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800803 // Check that symbols from non-shared libraries a shared library depends on are not visible
804 // from original namespace.
805
806 fn_t ns_get_internal_extern_string =
807 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
808 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
809 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
810 "ns_get_internal_extern_string() expected to return null but returns \"" <<
811 ns_get_internal_extern_string() << "\"";
812
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700813 dlclose(handle1);
814
815 // Check if handle2 is still alive (and well)
816 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
817 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
818 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
819 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
820
821 dlclose(handle2);
822}
823
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700824TEST(dlext, dlopen_ext_use_o_tmpfile_fd) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700825 const std::string lib_path = GetTestlibRoot() + "/libtest_simple.so";
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700826
827 int tmpfd = TEMP_FAILURE_RETRY(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700828 open(GetTestlibRoot().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL, 0));
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700829
830 // Ignore kernels without O_TMPFILE flag support
831 if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) {
832 return;
833 }
834
835 ASSERT_TRUE(tmpfd != -1) << strerror(errno);
836
837 android_namespace_t* ns =
838 android_create_namespace("testing-o_tmpfile",
839 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700840 GetTestlibRoot().c_str(),
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700841 ANDROID_NAMESPACE_TYPE_ISOLATED,
842 nullptr,
843 nullptr);
844
845 ASSERT_DL_NOTNULL(ns);
846
847 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
848
849 std::string content;
850 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
851 ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno);
852
853 android_dlextinfo extinfo;
854 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
855 extinfo.library_fd = tmpfd;
856 extinfo.library_namespace = ns;
857
858 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
859
860 ASSERT_DL_NOTNULL(handle);
861
862 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
863 ASSERT_DL_NOTNULL(taxicab_number);
864 EXPECT_EQ(1729U, *taxicab_number);
865 dlclose(handle);
866}
867
868TEST(dlext, dlopen_ext_use_memfd) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700869 const std::string lib_path = GetTestlibRoot() + "/libtest_simple.so";
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700870
871 // create memfd
872 int memfd = syscall(__NR_memfd_create, "foobar", MFD_CLOEXEC);
873 if (memfd == -1 && errno == ENOSYS) {
874 return;
875 }
876
877 ASSERT_TRUE(memfd != -1) << strerror(errno);
878
879 // Check st.f_type is TMPFS_MAGIC for memfd
880 struct statfs st;
881 ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno);
882 ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type);
883
884 android_namespace_t* ns =
885 android_create_namespace("testing-memfd",
886 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700887 GetTestlibRoot().c_str(),
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700888 ANDROID_NAMESPACE_TYPE_ISOLATED,
889 nullptr,
890 nullptr);
891
892 ASSERT_DL_NOTNULL(ns);
893
894 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
895
896 // read file into memfd backed one.
897 std::string content;
898 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
899 ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno);
900
901 android_dlextinfo extinfo;
902 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
903 extinfo.library_fd = memfd;
904 extinfo.library_namespace = ns;
905
906 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
907
908 ASSERT_DL_NOTNULL(handle);
909
910 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
911 ASSERT_DL_NOTNULL(taxicab_number);
912 EXPECT_EQ(1729U, *taxicab_number);
913 dlclose(handle);
914}
915
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800916TEST(dlext, ns_symbol_visibilty_one_namespace) {
917 static const char* root_lib = "libnstest_root.so";
918 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
919
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700920 const std::string ns_search_path = GetTestlibRoot() + "/public_namespace_libs:" +
921 GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800922
923 android_namespace_t* ns =
924 android_create_namespace("one",
925 nullptr,
926 ns_search_path.c_str(),
927 ANDROID_NAMESPACE_TYPE_ISOLATED,
928 nullptr,
929 nullptr);
930
931 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
932
933 android_dlextinfo extinfo;
934 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
935 extinfo.library_namespace = ns;
936
937 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
938 ASSERT_TRUE(handle != nullptr) << dlerror();
939
940 typedef const char* (*fn_t)();
941
942 // Check that relocation worked correctly
943 fn_t ns_get_internal_extern_string =
944 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
945 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
946 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
947
948 fn_t internal_extern_string_fn =
949 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
950 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
951 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
952}
953
954TEST(dlext, ns_symbol_visibilty_between_namespaces) {
955 static const char* root_lib = "libnstest_root.so";
956 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
957
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700958 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
959 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800960
961 android_namespace_t* ns_public =
962 android_create_namespace("public",
963 nullptr,
964 public_ns_search_path.c_str(),
965 ANDROID_NAMESPACE_TYPE_ISOLATED,
966 nullptr,
967 nullptr);
968
969 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
970
971 android_namespace_t* ns_private =
972 android_create_namespace("private",
973 nullptr,
974 private_ns_search_path.c_str(),
975 ANDROID_NAMESPACE_TYPE_ISOLATED,
976 nullptr,
977 nullptr);
978
979 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
980 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
981
982 android_dlextinfo extinfo;
983 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
984 extinfo.library_namespace = ns_private;
985
986 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
987 ASSERT_TRUE(handle != nullptr) << dlerror();
988
989 typedef const char* (*fn_t)();
990
991 // Check that relocation worked correctly
992 fn_t ns_get_internal_extern_string =
993 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
994 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
995 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
996 "ns_get_internal_extern_string() expected to return null but returns \"" <<
997 ns_get_internal_extern_string() << "\"";
998
999 fn_t internal_extern_string_fn =
1000 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
1001 ASSERT_TRUE(internal_extern_string_fn == nullptr);
1002 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
1003}
1004
1005TEST(dlext, ns_unload_between_namespaces) {
1006 static const char* root_lib = "libnstest_root.so";
1007 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1008
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001009 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1010 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001011
1012 android_namespace_t* ns_public =
1013 android_create_namespace("public",
1014 nullptr,
1015 public_ns_search_path.c_str(),
1016 ANDROID_NAMESPACE_TYPE_ISOLATED,
1017 nullptr,
1018 nullptr);
1019
1020 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1021
1022 android_namespace_t* ns_private =
1023 android_create_namespace("private",
1024 nullptr,
1025 private_ns_search_path.c_str(),
1026 ANDROID_NAMESPACE_TYPE_ISOLATED,
1027 nullptr,
1028 nullptr);
1029
1030 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
1031 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1032
1033 android_dlextinfo extinfo;
1034 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1035 extinfo.library_namespace = ns_private;
1036
1037 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1038 ASSERT_TRUE(handle != nullptr) << dlerror();
1039
1040 dlclose(handle);
1041 // Check that root_lib was unloaded
1042 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1043 ASSERT_TRUE(handle == nullptr);
1044 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
1045 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1046
1047 // Check that shared library was unloaded in public ns
1048 extinfo.library_namespace = ns_public;
1049 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1050 ASSERT_TRUE(handle == nullptr);
1051 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
1052 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1053}
1054
dimitry965d06d2017-11-28 16:03:07 +01001055TEST(dlext, ns_unload_between_namespaces_missing_symbol_direct) {
1056 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1057
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001058 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1059 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
dimitry965d06d2017-11-28 16:03:07 +01001060
1061 android_namespace_t* ns_public =
1062 android_create_namespace("public",
1063 nullptr,
1064 public_ns_search_path.c_str(),
1065 ANDROID_NAMESPACE_TYPE_ISOLATED,
1066 nullptr,
1067 nullptr);
1068
1069 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1070
1071 android_namespace_t* ns_private =
1072 android_create_namespace("private",
1073 nullptr,
1074 private_ns_search_path.c_str(),
1075 ANDROID_NAMESPACE_TYPE_ISOLATED,
1076 nullptr,
1077 nullptr);
1078
1079 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, "libtest_missing_symbol.so")) << dlerror();
1080 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1081
1082 android_dlextinfo extinfo;
1083 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1084 extinfo.library_namespace = ns_private;
1085
1086 void* handle = android_dlopen_ext((public_ns_search_path + "/libtest_missing_symbol.so").c_str(),
1087 RTLD_NOW,
1088 &extinfo);
1089 ASSERT_TRUE(handle == nullptr);
1090 ASSERT_EQ(std::string("dlopen failed: cannot locate symbol \"dlopen_testlib_missing_symbol\" referenced by \"") +
1091 public_ns_search_path + "/libtest_missing_symbol.so\"...",
1092 dlerror());
1093}
1094
1095TEST(dlext, ns_unload_between_namespaces_missing_symbol_indirect) {
1096 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1097
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001098 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1099 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
dimitry965d06d2017-11-28 16:03:07 +01001100
1101 android_namespace_t* ns_public =
1102 android_create_namespace("public",
1103 nullptr,
1104 public_ns_search_path.c_str(),
1105 ANDROID_NAMESPACE_TYPE_ISOLATED,
1106 nullptr,
1107 nullptr);
1108
1109 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1110
1111 android_namespace_t* ns_private =
1112 android_create_namespace("private",
1113 nullptr,
1114 private_ns_search_path.c_str(),
1115 ANDROID_NAMESPACE_TYPE_ISOLATED,
1116 nullptr,
1117 nullptr);
1118
1119 ASSERT_TRUE(android_link_namespaces(ns_private,
1120 ns_public,
1121 "libnstest_public.so:libtest_missing_symbol_child_public.so")
1122 ) << dlerror();
1123 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1124
1125 android_dlextinfo extinfo;
1126 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1127 extinfo.library_namespace = ns_private;
1128
1129 void* handle = android_dlopen_ext("libtest_missing_symbol_root.so", RTLD_NOW, &extinfo);
1130 ASSERT_TRUE(handle == nullptr);
1131 ASSERT_EQ(std::string("dlopen failed: cannot locate symbol \"dlopen_testlib_missing_symbol\" referenced by \"") +
1132 private_ns_search_path + "/libtest_missing_symbol_root.so\"...",
1133 dlerror());
1134}
1135
Jiyong Park37b91af2017-05-05 22:07:05 +09001136TEST(dlext, ns_greylist_enabled) {
Dimitry Ivanov18623142017-02-21 13:41:08 -08001137 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1138
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001139 const std::string ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov18623142017-02-21 13:41:08 -08001140
1141 android_namespace_t* ns =
1142 android_create_namespace("namespace",
1143 nullptr,
1144 ns_search_path.c_str(),
Jiyong Park37b91af2017-05-05 22:07:05 +09001145 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED,
Dimitry Ivanov18623142017-02-21 13:41:08 -08001146 nullptr,
1147 nullptr);
1148
1149 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1150
1151 android_dlextinfo extinfo;
1152 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1153 extinfo.library_namespace = ns;
1154
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001155 // An app targeting M can open libnativehelper.so because it's on the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001156 android_set_application_target_sdk_version(__ANDROID_API_M__);
1157 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1158 ASSERT_TRUE(handle != nullptr) << dlerror();
1159
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001160 // Check that loader did not load another copy of libdl.so while loading greylisted library.
1161 void* dlsym_ptr = dlsym(handle, "dlsym");
1162 ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror();
1163 ASSERT_EQ(&dlsym, dlsym_ptr);
1164
Dimitry Ivanov18623142017-02-21 13:41:08 -08001165 dlclose(handle);
1166
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001167 // An app targeting N no longer has the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001168 android_set_application_target_sdk_version(__ANDROID_API_N__);
1169 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1170 ASSERT_TRUE(handle == nullptr);
1171 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1172}
1173
Jiyong Park37b91af2017-05-05 22:07:05 +09001174TEST(dlext, ns_greylist_disabled_by_default) {
1175 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1176
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001177 const std::string ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Jiyong Park37b91af2017-05-05 22:07:05 +09001178
1179 android_namespace_t* ns =
1180 android_create_namespace("namespace",
1181 nullptr,
1182 ns_search_path.c_str(),
1183 ANDROID_NAMESPACE_TYPE_ISOLATED,
1184 nullptr,
1185 nullptr);
1186
1187 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1188
1189 android_dlextinfo extinfo;
1190 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1191 extinfo.library_namespace = ns;
1192
1193 android_set_application_target_sdk_version(__ANDROID_API_M__);
1194 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1195 ASSERT_TRUE(handle == nullptr);
1196 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1197}
1198
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001199TEST(dlext, ns_cyclic_namespaces) {
1200 // Test that ns1->ns2->ns1 link does not break the loader
1201 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1202 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
1203
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001204 const std::string ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001205
1206 android_namespace_t* ns1 =
1207 android_create_namespace("ns1",
1208 nullptr,
1209 ns_search_path.c_str(),
1210 ANDROID_NAMESPACE_TYPE_ISOLATED,
1211 nullptr,
1212 nullptr);
1213
1214 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
1215
1216 android_namespace_t* ns2 =
1217 android_create_namespace("ns1",
1218 nullptr,
1219 ns_search_path.c_str(),
1220 ANDROID_NAMESPACE_TYPE_ISOLATED,
1221 nullptr,
1222 nullptr);
1223
1224 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
1225
1226 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
1227 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
1228
1229 android_dlextinfo extinfo;
1230 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1231 extinfo.library_namespace = ns1;
1232
1233 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
1234 ASSERT_TRUE(handle == nullptr);
1235 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
1236}
1237
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001238TEST(dlext, ns_isolated) {
1239 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001240 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001241
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001242 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001243 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001244 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1245
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08001246 android_set_application_target_sdk_version(42U); // something > 23
1247
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001248 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001249
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001250 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001251 android_create_namespace("private",
1252 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001253 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001254 ANDROID_NAMESPACE_TYPE_REGULAR,
1255 nullptr,
1256 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001257 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001258 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001259
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001260 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001261 android_create_namespace("private_isolated1",
1262 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001263 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001264 ANDROID_NAMESPACE_TYPE_ISOLATED,
1265 nullptr,
1266 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001267 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001268 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001269
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001270 android_namespace_t* ns_isolated2 =
1271 android_create_namespace("private_isolated2",
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001272 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001273 nullptr,
1274 ANDROID_NAMESPACE_TYPE_ISOLATED,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001275 GetTestlibRoot().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001276 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001277 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001278 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001279
1280 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1281 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1282
1283 std::string lib_private_external_path =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001284 GetTestlibRoot() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001285
1286 // Load lib_private_external_path to default namespace
1287 // (it should remain invisible for the isolated namespaces after this)
1288 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1289 ASSERT_TRUE(handle != nullptr) << dlerror();
1290
1291 android_dlextinfo extinfo;
1292 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1293 extinfo.library_namespace = ns_not_isolated;
1294
1295 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1296 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1297
1298 extinfo.library_namespace = ns_isolated;
1299
1300 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1301 ASSERT_TRUE(handle2 == nullptr);
1302 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1303
1304 // Check dlopen by absolute path
1305 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1306 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001307 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001308 " or dlopened by \"" + android::base::GetExecutablePath() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001309 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001310
1311 extinfo.library_namespace = ns_isolated2;
1312
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001313 // this should work because isolation_path for private_isolated2 includes GetTestlibRoot()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001314 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001315 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1316 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001317
1318 // Check dlopen by absolute path
1319 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001320 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1321 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001322
1323 typedef const char* (*fn_t)();
1324 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1325 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1326
1327 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1328
1329 fn_t ns_get_private_extern_string =
1330 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1331 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1332
1333 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1334
1335 fn_t ns_get_public_extern_string =
1336 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1337 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1338
1339 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1340
1341 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1342 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1343
1344 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1345
1346 dlclose(handle1);
1347}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001348
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001349TEST(dlext, ns_shared) {
1350 static const char* root_lib = "libnstest_root_not_isolated.so";
1351 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001352
1353 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001354
Jiyong Park917d34a2017-08-31 14:07:13 +09001355 // create a parent namespace to use instead of the default namespace. This is
1356 // to make this test be independent from the configuration of the default
1357 // namespace.
1358 android_namespace_t* ns_parent =
1359 android_create_namespace("parent",
1360 nullptr,
1361 nullptr,
1362 ANDROID_NAMESPACE_TYPE_REGULAR,
1363 nullptr,
1364 nullptr);
1365 ASSERT_TRUE(ns_parent != nullptr) << dlerror();
1366 ASSERT_TRUE(android_link_namespaces(ns_parent, nullptr, g_core_shared_libs.c_str())) << dlerror();
1367
1368 android_dlextinfo extinfo;
1369 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1370 extinfo.library_namespace = ns_parent;
1371
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001372 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Jiyong Park917d34a2017-08-31 14:07:13 +09001373 void* handle_public = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001374 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1375
1376 android_set_application_target_sdk_version(42U); // something > 23
1377
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001378 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001379
Jiyong Park917d34a2017-08-31 14:07:13 +09001380 // preload this library to the parent namespace to check if it
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001381 // is shared later on.
1382 void* handle_dlopened =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001383 android_dlopen_ext((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001384 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1385
Jiyong Park917d34a2017-08-31 14:07:13 +09001386 // create two child namespaces of 'ns_parent'. One with regular, the other
1387 // with isolated & shared.
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001388 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001389 android_create_namespace("private",
1390 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001391 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001392 ANDROID_NAMESPACE_TYPE_REGULAR,
1393 nullptr,
Jiyong Park917d34a2017-08-31 14:07:13 +09001394 ns_parent);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001395 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Jiyong Park917d34a2017-08-31 14:07:13 +09001396 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, ns_parent, g_public_lib)) << dlerror();
1397 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001398
1399 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001400 android_create_namespace("private_isolated_shared",
1401 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001402 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001403 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001404 nullptr,
Jiyong Park917d34a2017-08-31 14:07:13 +09001405 ns_parent);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001406 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Jiyong Park917d34a2017-08-31 14:07:13 +09001407 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, ns_parent, g_public_lib)) << dlerror();
1408 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001409
Jiyong Park917d34a2017-08-31 14:07:13 +09001410 ASSERT_TRUE(android_dlopen_ext(root_lib, RTLD_NOW, &extinfo) == nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001411 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1412
1413 std::string lib_private_external_path =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001414 GetTestlibRoot() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001415
Jiyong Park917d34a2017-08-31 14:07:13 +09001416 // Load lib_private_external_path to the parent namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001417 // (it should remain invisible for the isolated namespaces after this)
Jiyong Park917d34a2017-08-31 14:07:13 +09001418 void* handle = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001419 ASSERT_TRUE(handle != nullptr) << dlerror();
1420
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001421 extinfo.library_namespace = ns_not_isolated;
1422
1423 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1424 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1425
1426 extinfo.library_namespace = ns_isolated_shared;
1427
1428 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1429 ASSERT_TRUE(handle2 == nullptr);
1430 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1431
1432 // Check dlopen by absolute path
1433 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1434 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001435 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001436 " or dlopened by \"" + android::base::GetExecutablePath() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001437 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001438
1439 // load libnstest_root.so to shared namespace in order to check that everything is different
1440 // except shared libnstest_dlopened.so
1441
1442 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1443
1444 typedef const char* (*fn_t)();
1445 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1446 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1447 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1448 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1449
1450 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1451 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1452 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1453
1454 fn_t ns_get_private_extern_string =
1455 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1456 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1457 fn_t ns_get_private_extern_string_shared =
1458 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1459 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1460
1461 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1462 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1463 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1464
1465 fn_t ns_get_public_extern_string =
1466 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1467 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1468 fn_t ns_get_public_extern_string_shared =
1469 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1470 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1471
1472 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1473 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1474 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1475
1476 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1477 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1478 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1479 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1480 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1481 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1482
1483 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1484 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1485 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1486 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1487 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1488
1489 dlclose(handle1);
1490 dlclose(handle2);
1491}
1492
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001493TEST(dlext, ns_shared_links_and_paths) {
1494 // Create parent namespace (isolated, not shared)
1495 android_namespace_t* ns_isolated =
1496 android_create_namespace("private_isolated",
1497 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001498 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001499 ANDROID_NAMESPACE_TYPE_ISOLATED,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001500 (GetTestlibRoot() + "/public_namespace_libs").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001501 nullptr);
1502 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
1503 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
1504
1505 // Create shared namespace with ns_isolated parent
1506 android_namespace_t* ns_shared =
1507 android_create_namespace("private_shared",
1508 nullptr,
1509 nullptr,
1510 ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED,
1511 nullptr,
1512 ns_isolated);
1513 ASSERT_TRUE(ns_shared != nullptr) << dlerror();
1514
1515 // 1. Load a library in ns_shared to check that it has inherited
1516 // search path and the link to the default namespace.
1517 android_dlextinfo extinfo;
1518 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1519 extinfo.library_namespace = ns_shared;
1520
1521 {
1522 void* handle = android_dlopen_ext("libnstest_private.so", RTLD_NOW, &extinfo);
1523 ASSERT_TRUE(handle != nullptr) << dlerror();
1524 const char** ns_private_extern_string = static_cast<const char**>(dlsym(handle, "g_private_extern_string"));
1525 ASSERT_TRUE(ns_private_extern_string != nullptr) << dlerror();
1526 ASSERT_STREQ("This string is from private namespace", *ns_private_extern_string);
1527
1528 dlclose(handle);
1529 }
1530 // 2. Load another test library by absolute path to check that
1531 // it has inherited permitted_when_isolated_path
1532 {
1533 void* handle = android_dlopen_ext(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001534 (GetTestlibRoot() + "/public_namespace_libs/libnstest_public.so").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001535 RTLD_NOW,
1536 &extinfo);
1537
1538 ASSERT_TRUE(handle != nullptr) << dlerror();
1539 const char** ns_public_extern_string = static_cast<const char**>(dlsym(handle, "g_public_extern_string"));
1540 ASSERT_TRUE(ns_public_extern_string != nullptr) << dlerror();
1541 ASSERT_STREQ("This string is from public namespace", *ns_public_extern_string);
1542
1543 dlclose(handle);
1544 }
1545
1546 // 3. Check that it is still isolated.
1547 {
1548 void* handle = android_dlopen_ext(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001549 (GetTestlibRoot() + "/libtest_empty.so").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001550 RTLD_NOW,
1551 &extinfo);
1552
1553 ASSERT_TRUE(handle == nullptr);
1554 }
1555}
1556
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001557TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001558 android_set_application_target_sdk_version(42U); // something > 23
1559
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001560 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001561
1562 // preload this library to the default namespace to check if it
1563 // is shared later on.
1564 void* handle_dlopened =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001565 dlopen((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001566 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1567
1568 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001569 android_create_namespace("private_isolated_shared",
1570 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001571 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001572 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001573 nullptr,
1574 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001575 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001576 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001577
1578 // Check if "libnstest_dlopened.so" is loaded (and the same)
1579 android_dlextinfo extinfo;
1580 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1581 extinfo.library_namespace = ns_isolated_shared;
1582
1583 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1584 ASSERT_TRUE(handle != nullptr) << dlerror();
1585 ASSERT_TRUE(handle == handle_dlopened);
1586 dlclose(handle);
1587 dlclose(handle_dlopened);
1588
1589 // And now check that the library cannot be found by soname (and is no longer loaded)
1590 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1591 ASSERT_TRUE(handle == nullptr)
1592 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1593
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001594 handle = android_dlopen_ext((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001595 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1596 ASSERT_TRUE(handle == nullptr)
1597 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1598
1599 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1600 ASSERT_TRUE(handle == nullptr)
1601 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1602
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001603 handle = dlopen((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001604 RTLD_NOW | RTLD_NOLOAD);
1605 ASSERT_TRUE(handle == nullptr)
1606 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1607
1608 // Now lets see if the soinfo area gets reused in the wrong way:
1609 // load a library to default namespace.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001610 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001611 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1612 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1613
1614 // try to find it in shared namespace
1615 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1616 ASSERT_TRUE(handle == nullptr)
1617 << "Error: " << g_public_lib << " is accessible in shared namespace";
1618}
1619
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001620TEST(dlext, ns_isolated_rtld_global) {
1621 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001622 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001623
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001624 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001625
1626 android_namespace_t* ns1 =
1627 android_create_namespace("isolated1",
1628 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001629 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001630 ANDROID_NAMESPACE_TYPE_ISOLATED,
1631 lib_public_path.c_str(),
1632 nullptr);
1633 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001634 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001635
1636 android_namespace_t* ns2 =
1637 android_create_namespace("isolated2",
1638 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001639 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001640 ANDROID_NAMESPACE_TYPE_ISOLATED,
1641 lib_public_path.c_str(),
1642 nullptr);
1643 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001644 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001645
1646 android_dlextinfo extinfo;
1647 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1648 extinfo.library_namespace = ns1;
1649
1650 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1651 RTLD_GLOBAL,
1652 &extinfo);
1653
1654 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1655
1656 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001657 android_create_namespace("isolated1_child",
1658 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001659 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001660 ANDROID_NAMESPACE_TYPE_ISOLATED,
1661 nullptr,
1662 ns1);
1663
1664 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1665 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001666
1667 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1668 // attempt to use ns2 should result in dlerror()
1669
1670 // Check ns1_child first.
1671 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1672 extinfo.library_namespace = ns1_child;
1673
1674 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1675 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1676
1677 // now ns1
1678 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1679 extinfo.library_namespace = ns1;
1680
1681 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1682 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1683
1684 // and ns2 should fail
1685 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1686 extinfo.library_namespace = ns2;
1687
1688 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1689 ASSERT_TRUE(handle1 == nullptr);
1690 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1691}
1692
dimitry8db36a52017-10-23 15:10:10 +02001693TEST(dlext, ns_inaccessible_error_message) {
1694 // We set up 2 namespaces (a and b) and link a->b with a shared library
1695 // libtestshared.so. Then try to dlopen different library with the same
1696 // name from in namespace a. Note that library should not be accessible
1697 // in either namespace but since it's soname is in the list of shared libs
1698 // the linker will attempt to find it in linked namespace.
1699 //
1700 // Check the error message and make sure it mentions correct namespace name.
1701 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1702
1703 android_namespace_t* ns_a =
1704 android_create_namespace("ns_a",
1705 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001706 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
dimitry8db36a52017-10-23 15:10:10 +02001707 ANDROID_NAMESPACE_TYPE_ISOLATED,
1708 nullptr,
1709 nullptr);
1710 ASSERT_TRUE(ns_a != nullptr) << dlerror();
1711 ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
1712
1713 android_namespace_t* ns_b =
1714 android_create_namespace("ns_b",
1715 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001716 GetTestlibRoot().c_str(),
dimitry8db36a52017-10-23 15:10:10 +02001717 ANDROID_NAMESPACE_TYPE_ISOLATED,
1718 nullptr,
1719 nullptr);
1720 ASSERT_TRUE(ns_b != nullptr) << dlerror();
1721 ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
1722
1723 ASSERT_TRUE(android_link_namespaces(ns_a, ns_b, "libtestshared.so")) << dlerror();
1724
1725 android_dlextinfo extinfo;
1726 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1727 extinfo.library_namespace = ns_a;
1728
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001729 std::string library_path = GetTestlibRoot() + "/inaccessible_libs/libtestshared.so";
dimitry8db36a52017-10-23 15:10:10 +02001730
1731 void* handle = android_dlopen_ext(library_path.c_str(), RTLD_NOW, &extinfo);
1732 ASSERT_TRUE(handle == nullptr);
1733 std::string expected_dlerror =
1734 android::base::StringPrintf("dlopen failed: library \"%s\" needed or dlopened by \"%s\""
1735 " is not accessible for the namespace \"ns_a\"",
1736 library_path.c_str(),
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001737 android::base::GetExecutablePath().c_str());
dimitry8db36a52017-10-23 15:10:10 +02001738 ASSERT_EQ(expected_dlerror, dlerror());
1739}
1740
dimitry321476a2018-01-29 15:32:37 +01001741extern "C" bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
1742 android_namespace_t* namespace_to);
1743
Logan Chien9ee45912018-01-18 12:05:09 +08001744TEST(dlext, ns_link_namespaces_invalid_arguments) {
1745 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1746
1747 android_namespace_t* ns =
1748 android_create_namespace("private",
1749 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001750 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001751 ANDROID_NAMESPACE_TYPE_REGULAR,
1752 nullptr,
1753 nullptr);
1754 ASSERT_TRUE(ns != nullptr) << dlerror();
1755
1756 // Test android_link_namespaces()
1757 ASSERT_FALSE(android_link_namespaces(nullptr, nullptr, "libc.so"));
1758 ASSERT_STREQ("android_link_namespaces failed: error linking namespaces: namespace_from is null.",
1759 dlerror());
1760
1761 ASSERT_FALSE(android_link_namespaces(ns, nullptr, nullptr));
1762 ASSERT_STREQ("android_link_namespaces failed: "
1763 "error linking namespaces \"private\"->\"(default)\": "
1764 "the list of shared libraries is empty.", dlerror());
1765
1766 ASSERT_FALSE(android_link_namespaces(ns, nullptr, ""));
1767 ASSERT_STREQ("android_link_namespaces failed: "
1768 "error linking namespaces \"private\"->\"(default)\": "
1769 "the list of shared libraries is empty.", dlerror());
1770
dimitry321476a2018-01-29 15:32:37 +01001771 // Test __loader_android_link_namespaces_all_libs()
1772 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(nullptr, nullptr));
Logan Chien9ee45912018-01-18 12:05:09 +08001773 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1774 "error linking namespaces: namespace_from is null.", dlerror());
1775
dimitry321476a2018-01-29 15:32:37 +01001776 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(nullptr, ns));
Logan Chien9ee45912018-01-18 12:05:09 +08001777 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1778 "error linking namespaces: namespace_from is null.", dlerror());
1779
dimitry321476a2018-01-29 15:32:37 +01001780 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(ns, nullptr));
Logan Chien9ee45912018-01-18 12:05:09 +08001781 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1782 "error linking namespaces: namespace_to is null.", dlerror());
1783}
1784
1785TEST(dlext, ns_allow_all_shared_libs) {
1786 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1787
1788 android_namespace_t* ns_a =
1789 android_create_namespace("ns_a",
1790 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001791 (GetTestlibRoot() + "/ns_a").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001792 ANDROID_NAMESPACE_TYPE_ISOLATED,
1793 nullptr,
1794 nullptr);
1795 ASSERT_TRUE(ns_a != nullptr) << dlerror();
1796 ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
1797
1798 android_namespace_t* ns_b =
1799 android_create_namespace("ns_b",
1800 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001801 (GetTestlibRoot() + "/ns_b").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001802 ANDROID_NAMESPACE_TYPE_ISOLATED,
1803 nullptr,
1804 nullptr);
1805 ASSERT_TRUE(ns_b != nullptr) << dlerror();
1806 ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
1807
1808 ASSERT_TRUE(android_link_namespaces(ns_b, ns_a, "libnstest_ns_a_public1.so")) << dlerror();
dimitry321476a2018-01-29 15:32:37 +01001809 ASSERT_TRUE(__loader_android_link_namespaces_all_libs(ns_a, ns_b)) << dlerror();
Logan Chien9ee45912018-01-18 12:05:09 +08001810
1811 // Load libs with android_dlopen_ext() from namespace b
1812 android_dlextinfo extinfo;
1813 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1814 extinfo.library_namespace = ns_b;
1815
1816 void* ns_b_handle1 = android_dlopen_ext("libnstest_ns_a_public1.so", RTLD_NOW, &extinfo);
1817 ASSERT_TRUE(ns_b_handle1 != nullptr) << dlerror();
1818
1819 void* ns_b_handle1_internal =
1820 android_dlopen_ext("libnstest_ns_a_public1_internal.so", RTLD_NOW, &extinfo);
1821 ASSERT_TRUE(ns_b_handle1_internal == nullptr);
1822
1823 void* ns_b_handle2 = android_dlopen_ext("libnstest_ns_b_public2.so", RTLD_NOW, &extinfo);
1824 ASSERT_TRUE(ns_b_handle2 != nullptr) << dlerror();
1825
1826 void* ns_b_handle3 = android_dlopen_ext("libnstest_ns_b_public3.so", RTLD_NOW, &extinfo);
1827 ASSERT_TRUE(ns_b_handle3 != nullptr) << dlerror();
1828
1829 // Load libs with android_dlopen_ext() from namespace a
1830 extinfo.library_namespace = ns_a;
1831
1832 void* ns_a_handle1 = android_dlopen_ext("libnstest_ns_a_public1.so", RTLD_NOW, &extinfo);
1833 ASSERT_TRUE(ns_a_handle1 != nullptr) << dlerror();
1834
1835 void* ns_a_handle1_internal =
1836 android_dlopen_ext("libnstest_ns_a_public1_internal.so", RTLD_NOW, &extinfo);
1837 ASSERT_TRUE(ns_a_handle1_internal != nullptr) << dlerror();
1838
1839 void* ns_a_handle2 = android_dlopen_ext("libnstest_ns_b_public2.so", RTLD_NOW, &extinfo);
1840 ASSERT_TRUE(ns_a_handle2 != nullptr) << dlerror();
1841
1842 void* ns_a_handle3 = android_dlopen_ext("libnstest_ns_b_public3.so", RTLD_NOW, &extinfo);
1843 ASSERT_TRUE(ns_a_handle3 != nullptr) << dlerror();
1844
1845 // Compare the dlopen handle
1846 ASSERT_EQ(ns_b_handle1, ns_a_handle1);
1847 ASSERT_EQ(ns_b_handle2, ns_a_handle2);
1848 ASSERT_EQ(ns_b_handle3, ns_a_handle3);
1849
1850 // Close libs
1851 dlclose(ns_b_handle1);
1852 dlclose(ns_b_handle2);
1853 dlclose(ns_b_handle3);
1854
1855 dlclose(ns_a_handle1);
1856 dlclose(ns_a_handle1_internal);
1857 dlclose(ns_a_handle2);
1858 dlclose(ns_a_handle3);
1859}
1860
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001861TEST(dlext, ns_anonymous) {
1862 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001863 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001864
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001865 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001866 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1867
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001868 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1869
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001870 ASSERT_TRUE(
1871 android_init_anonymous_namespace(shared_libs.c_str(),
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001872 (GetTestlibRoot() + "/private_namespace_libs").c_str())
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001873 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001874
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001875 android_namespace_t* ns =
1876 android_create_namespace("private",
1877 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001878 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001879 ANDROID_NAMESPACE_TYPE_REGULAR,
1880 nullptr,
1881 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001882
1883 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001884 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001885
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001886 std::string private_library_absolute_path = GetTestlibRoot() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001887
1888 android_dlextinfo extinfo;
1889 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1890 extinfo.library_namespace = ns;
1891
1892 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1893 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1894 ASSERT_TRUE(handle != nullptr) << dlerror();
1895
1896 uintptr_t ns_get_dlopened_string_addr =
1897 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1898 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1899 typedef const char* (*fn_t)();
1900 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1901
1902 std::vector<map_record> maps;
1903 Maps::parse_maps(&maps);
1904
1905 uintptr_t addr_start = 0;
1906 uintptr_t addr_end = 0;
dimitry8eaf28d2017-10-11 10:04:14 +02001907 bool has_executable_segment = false;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001908 std::vector<map_record> maps_to_copy;
1909
1910 for (const auto& rec : maps) {
1911 if (rec.pathname == private_library_absolute_path) {
1912 if (addr_start == 0) {
1913 addr_start = rec.addr_start;
1914 }
1915 addr_end = rec.addr_end;
dimitry8eaf28d2017-10-11 10:04:14 +02001916 has_executable_segment = has_executable_segment || (rec.perms & PROT_EXEC) != 0;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001917
1918 maps_to_copy.push_back(rec);
1919 }
1920 }
1921
1922 // some sanity checks..
1923 ASSERT_TRUE(addr_start > 0);
1924 ASSERT_TRUE(addr_end > 0);
dimitry3b0a5b72018-06-06 11:11:25 +02001925 ASSERT_TRUE(maps_to_copy.size() > 0);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001926 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1927 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1928
dimitry8eaf28d2017-10-11 10:04:14 +02001929 if (!has_executable_segment) {
1930 // For some natively bridged environments this code might be missing
1931 // the executable flag. This is because the guest code is not supposed
1932 // to be executed directly and making it non-executable is more secure.
1933 // If this is the case we assume that the first segment is the one that
1934 // has this flag.
1935 ASSERT_TRUE((maps_to_copy[0].perms & PROT_WRITE) == 0);
1936 maps_to_copy[0].perms |= PROT_EXEC;
1937 }
1938
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001939 // copy
1940 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1941 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1942 -1, 0));
1943 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1944
1945 for (const auto& rec : maps_to_copy) {
1946 uintptr_t offset = rec.addr_start - addr_start;
1947 size_t size = rec.addr_end - rec.addr_start;
1948 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1949 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1950 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1951 ASSERT_TRUE(map != MAP_FAILED);
1952 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1953 mprotect(map, size, rec.perms);
1954 }
1955
1956 // call the function copy
1957 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1958 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1959 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1960 ns_get_dlopened_string_anon());
1961
1962 // They should belong to different namespaces (private and anonymous)
1963 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1964 ns_get_dlopened_string_private());
1965
1966 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1967}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001968
1969TEST(dlext, dlopen_handle_value_platform) {
1970 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1971 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1972 << "dlopen should return odd value for the handle";
1973 dlclose(handle);
1974}
1975
1976TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001977 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001978 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1979 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1980 << "dlopen should return valid pointer";
1981 dlclose(handle);
1982}