blob: cf642cdf16506293f24c3d9e7d95ca3fbb5ffe58 [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>
Zhenhua WANG81aad002017-04-25 11:07:19 +080029#include <android-base/strings.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070030
31#include <linux/memfd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000032#include <sys/mman.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010033#include <sys/types.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070034#include <sys/vfs.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000035#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000036
Torne (Richard Coles)26052612014-05-02 14:57:42 +010037#include <pagemap/pagemap.h>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080038#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010039
Dimitry Ivanov927877c2016-09-21 11:17:13 -070040#include "gtest_globals.h"
Yabin Cui294d1e22014-12-07 20:43:37 -080041#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080042#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070043#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070044#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000045
46#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070047 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000048
49#define ASSERT_DL_ZERO(i) \
50 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
51
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000052#define ASSERT_NOERROR(i) \
53 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
54
Yabin Cui16f7f8d2014-11-04 11:08:05 -080055#define ASSERT_SUBSTR(needle, haystack) \
56 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
57
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000058
59typedef int (*fn)(void);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070060constexpr const char* kLibName = "libdlext_test.so";
61constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
62constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
63constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070064
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000065class DlExtTest : public ::testing::Test {
66protected:
67 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070068 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000069 // verify that we don't have the library loaded already
Dimitry Ivanov927877c2016-09-21 11:17:13 -070070 void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070071 ASSERT_TRUE(h == nullptr);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070072 h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070073 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000074 // call dlerror() to swallow the error, and check it was the one we wanted
Dimitry Ivanov927877c2016-09-21 11:17:13 -070075 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 +000076 }
77
78 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070079 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000080 ASSERT_DL_ZERO(dlclose(handle_));
81 }
82 }
83
84 void* handle_;
85};
86
87TEST_F(DlExtTest, ExtInfoNull) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -070088 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000089 ASSERT_DL_NOTNULL(handle_);
90 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
91 ASSERT_DL_NOTNULL(f);
92 EXPECT_EQ(4, f());
93}
94
95TEST_F(DlExtTest, ExtInfoNoFlags) {
96 android_dlextinfo extinfo;
97 extinfo.flags = 0;
Dimitry Ivanov927877c2016-09-21 11:17:13 -070098 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000099 ASSERT_DL_NOTNULL(handle_);
100 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
101 ASSERT_DL_NOTNULL(f);
102 EXPECT_EQ(4, f());
103}
104
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700105TEST_F(DlExtTest, ExtInfoUseFd) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800106 const std::string lib_path = get_testlib_root() + "/libdlext_test_fd/libdlext_test_fd.so";
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700107
108 android_dlextinfo extinfo;
109 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700110 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700111 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700112 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700113 ASSERT_DL_NOTNULL(handle_);
114 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
115 ASSERT_DL_NOTNULL(f);
116 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700117
118 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
119 ASSERT_DL_NOTNULL(taxicab_number);
120 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700121}
122
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700123TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800124 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700125
126 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700127 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700128 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800129
130 // Find the offset of the shared library in the zip.
131 ZipArchiveHandle handle;
132 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
133 ZipEntry zip_entry;
134 ZipString zip_name;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700135 zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
136 zip_name.name_length = strlen(kLibZipSimpleZip);
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800137 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
138 extinfo.library_fd_offset = zip_entry.offset;
139 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700140
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700141 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700142 ASSERT_DL_NOTNULL(handle_);
143
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700144 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
145 ASSERT_DL_NOTNULL(taxicab_number);
146 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700147}
148
149TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800150 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700151
152 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700153 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700154 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700155 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700156
157 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
158 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
160
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800161 // Test an address above 2^44, for http://b/18178121 .
162 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700163 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700164 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800165 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
166
167 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
168 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
169 ASSERT_TRUE(handle_ == nullptr);
170 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
171
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700172 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700173 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800174 ASSERT_TRUE(handle_ == nullptr);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700175 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700176
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800177 // Check if dlsym works after unsuccessful dlopen().
178 // Supply non-exiting one to make linker visit every soinfo.
179 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
180 ASSERT_TRUE(sym == nullptr);
181
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700183}
184
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800185TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700186 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700187 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800188 // This offset will not be used, so it doesn't matter.
189 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700190
191 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
192 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700193 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 -0700194}
195
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700196TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700197 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
198 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700199 // 1. Open actual file
200 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
201 ASSERT_DL_NOTNULL(handle);
202 // 2. Open link with force_load flag set
203 android_dlextinfo extinfo;
204 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700205 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700206 ASSERT_DL_NOTNULL(handle2);
207 ASSERT_TRUE(handle != handle2);
208
209 dlclose(handle2);
210 dlclose(handle);
211}
212
213TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700214 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
215 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700216 // Check if soname lookup still returns already loaded library
217 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700218 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700219 ASSERT_DL_NOTNULL(handle);
220
221 android_dlextinfo extinfo;
222 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
223
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700224 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700225 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
226
227 ASSERT_DL_NOTNULL(handle2);
228 ASSERT_TRUE(handle == handle2);
229
230 dlclose(handle2);
231 dlclose(handle);
232}
233
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700234TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700235 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800236 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700237
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700238 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700239 ASSERT_TRUE(handle != nullptr) << dlerror();
240
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700241 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
242 ASSERT_DL_NOTNULL(taxicab_number);
243 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700244
245 dlclose(handle);
246}
247
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700248TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700249 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800250 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700251
252 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
253
254 ASSERT_TRUE(handle != nullptr) << dlerror();
255
256 typedef void *(* dlopen_b_fn)();
257 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
258 ASSERT_TRUE(fn != nullptr) << dlerror();
259
260 void *p = fn();
261 ASSERT_TRUE(p != nullptr) << dlerror();
262
263 dlclose(p);
264 dlclose(handle);
265}
266
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700267TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700268 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800269 const std::string lib_path = get_testlib_root() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700270
271 typedef void (*fn_t)(const char*);
272 fn_t android_update_LD_LIBRARY_PATH =
273 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
274
275 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
276
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700277 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700278 ASSERT_TRUE(handle == nullptr);
279
280 android_update_LD_LIBRARY_PATH(lib_path.c_str());
281
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700282 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700283 ASSERT_TRUE(handle != nullptr) << dlerror();
284
285 int (*fn)(void);
286 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
287 ASSERT_TRUE(fn != nullptr);
288 EXPECT_EQ(4, fn());
289
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800290 uint32_t* taxicab_number =
291 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700292 ASSERT_DL_NOTNULL(taxicab_number);
293 EXPECT_EQ(1729U, *taxicab_number);
294
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700295 dlclose(handle);
296}
297
298
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000299TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700300 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000301 ASSERT_TRUE(start != MAP_FAILED);
302 android_dlextinfo extinfo;
303 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
304 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700305 extinfo.reserved_size = kLibSize;
306 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000307 ASSERT_DL_NOTNULL(handle_);
308 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
309 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700310 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000311 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700312 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000313 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800314
315 // Check that after dlclose reserved address space is unmapped (and can be reused)
316 dlclose(handle_);
317 handle_ = nullptr;
318
319 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
320 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000321}
322
323TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800324 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000325 ASSERT_TRUE(start != MAP_FAILED);
326 android_dlextinfo extinfo;
327 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
328 extinfo.reserved_addr = start;
329 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700330 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700331 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000332}
333
334TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700335 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000336 ASSERT_TRUE(start != MAP_FAILED);
337 android_dlextinfo extinfo;
338 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
339 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700340 extinfo.reserved_size = kLibSize;
341 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000342 ASSERT_DL_NOTNULL(handle_);
343 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
344 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700345 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000346 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700347 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000348 EXPECT_EQ(4, f());
349}
350
351TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800352 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000353 ASSERT_TRUE(start != MAP_FAILED);
354 android_dlextinfo extinfo;
355 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
356 extinfo.reserved_addr = start;
357 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700358 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000359 ASSERT_DL_NOTNULL(handle_);
360 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
361 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700362 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
363 (reinterpret_cast<void*>(f) >=
364 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000365 EXPECT_EQ(4, f());
366}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000367
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700368TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700369 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700370 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700371 munmap(start, kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700372
373 android_dlextinfo extinfo;
374 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
375 extinfo.reserved_addr = start;
376
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700377 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700378 ASSERT_DL_NOTNULL(handle_);
379 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
380 ASSERT_DL_NOTNULL(f);
381 EXPECT_GE(reinterpret_cast<void*>(f), start);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700382 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700383
384 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800385 dlclose(handle_);
386 handle_ = nullptr;
387
388 // Check that dlclose unmapped the file
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700389 void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800390 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700391}
392
393TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700394 void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700395 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
396 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700397 munmap(start, kLibSize + PAGE_SIZE);
398 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700399 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
400 ASSERT_TRUE(new_addr != MAP_FAILED);
401
402 android_dlextinfo extinfo;
403 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
404 extinfo.reserved_addr = start;
405
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700406 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700407 ASSERT_TRUE(handle_ == nullptr);
408}
409
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410class DlExtRelroSharingTest : public DlExtTest {
411protected:
412 virtual void SetUp() {
413 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700414 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100415 ASSERT_TRUE(start != MAP_FAILED);
416 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
417 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700418 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100419 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000420 }
421
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100422 virtual void TearDown() {
423 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100424 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000425
Yabin Cui294d1e22014-12-07 20:43:37 -0800426 void CreateRelroFile(const char* lib, const char* relro_file) {
427 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100428 ASSERT_NOERROR(relro_fd);
429
430 pid_t pid = fork();
431 if (pid == 0) {
432 // child process
433 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
434 extinfo_.relro_fd = relro_fd;
435 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700436 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100437 fprintf(stderr, "in child: %s\n", dlerror());
438 exit(1);
439 }
440 exit(0);
441 }
442
443 // continuing in parent
444 ASSERT_NOERROR(close(relro_fd));
445 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800446 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100447
448 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800449 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100450 ASSERT_NOERROR(relro_fd);
451 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
452 extinfo_.relro_fd = relro_fd;
453 }
454
455 void TryUsingRelro(const char* lib) {
456 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
457 ASSERT_DL_NOTNULL(handle_);
458 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
459 ASSERT_DL_NOTNULL(f);
460 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700461
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800462 uint32_t* taxicab_number =
463 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700464 ASSERT_DL_NOTNULL(taxicab_number);
465 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100466 }
467
Zhenhua WANG81aad002017-04-25 11:07:19 +0800468 void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro,
469 size_t* pss_out);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100470
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100471 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100472};
473
474TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800475 TemporaryFile tf; // Use tf to get an unique filename.
476 ASSERT_NOERROR(close(tf.fd));
477
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700478 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
479 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Yabin Cui294d1e22014-12-07 20:43:37 -0800480
481 // Use destructor of tf to close and unlink the file.
482 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100483}
484
485TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800486 TemporaryFile tf; // // Use tf to get an unique filename.
487 ASSERT_NOERROR(close(tf.fd));
488
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700489 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
490 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
Yabin Cui294d1e22014-12-07 20:43:37 -0800491
492 // Use destructor of tf to close and unlink the file.
493 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100494}
495
496TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700497 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000498}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100499
500TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700501 if (geteuid() != 0) {
502 GTEST_LOG_(INFO) << "This test must be run as root.\n";
503 return;
504 }
505
Yabin Cui294d1e22014-12-07 20:43:37 -0800506 TemporaryFile tf; // Use tf to get an unique filename.
507 ASSERT_NOERROR(close(tf.fd));
508
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700509 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
Yabin Cui294d1e22014-12-07 20:43:37 -0800510
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100511 int pipefd[2];
512 ASSERT_NOERROR(pipe(pipefd));
513
514 size_t without_sharing, with_sharing;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800515 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, false, &without_sharing));
516 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, true, &with_sharing));
517 ASSERT_LT(with_sharing, without_sharing);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100518
Zhenhua WANG81aad002017-04-25 11:07:19 +0800519 // We expect the sharing to save at least 50% of the library's total PSS.
520 // In practice it saves 80%+ for this library in the test.
521 size_t pss_saved = without_sharing - with_sharing;
522 size_t expected_min_saved = without_sharing / 2;
523
524 EXPECT_LT(expected_min_saved, pss_saved);
Yabin Cui294d1e22014-12-07 20:43:37 -0800525
526 // Use destructor of tf to close and unlink the file.
527 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100528}
529
Zhenhua WANG81aad002017-04-25 11:07:19 +0800530void GetPss(bool shared_relro, const char* lib, const char* relro_file, pid_t pid,
531 size_t* total_pss) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100532 pm_kernel_t* kernel;
533 ASSERT_EQ(0, pm_kernel_create(&kernel));
534
535 pm_process_t* process;
536 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
537
538 pm_map_t** maps;
539 size_t num_maps;
540 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
541
Zhenhua WANG81aad002017-04-25 11:07:19 +0800542 // Calculate total PSS of the library.
543 *total_pss = 0;
544 bool saw_relro_file = false;
545 for (size_t i = 0; i < num_maps; ++i) {
546 if (android::base::EndsWith(maps[i]->name, lib) || strcmp(maps[i]->name, relro_file) == 0) {
547 if (strcmp(maps[i]->name, relro_file) == 0) saw_relro_file = true;
548
549 pm_memusage_t usage;
550 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
551 *total_pss += usage.pss;
552 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100553 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100554
555 free(maps);
556 pm_process_destroy(process);
557 pm_kernel_destroy(kernel);
Zhenhua WANG81aad002017-04-25 11:07:19 +0800558
559 if (shared_relro) ASSERT_TRUE(saw_relro_file);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100560}
561
Zhenhua WANG81aad002017-04-25 11:07:19 +0800562void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file,
563 bool share_relro, size_t* pss_out) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100564 const int CHILDREN = 20;
565
566 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800567 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100568 int childpipe[CHILDREN];
569 for (int i=0; i<CHILDREN; ++i) {
570 char read_buf;
571 int child_done_pipe[2], parent_done_pipe[2];
572 ASSERT_NOERROR(pipe(child_done_pipe));
573 ASSERT_NOERROR(pipe(parent_done_pipe));
574
575 pid_t child = fork();
576 if (child == 0) {
577 // close the 'wrong' ends of the pipes in the child
578 close(child_done_pipe[0]);
579 close(parent_done_pipe[1]);
580
581 // open the library
582 void* handle;
583 if (share_relro) {
584 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
585 } else {
586 handle = dlopen(lib, RTLD_NOW);
587 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700588 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100589 fprintf(stderr, "in child: %s\n", dlerror());
590 exit(1);
591 }
592
593 // close write end of child_done_pipe to signal the parent that we're done.
594 close(child_done_pipe[1]);
595
596 // wait for the parent to close parent_done_pipe, then exit
597 read(parent_done_pipe[0], &read_buf, 1);
598 exit(0);
599 }
600
601 ASSERT_NOERROR(child);
602
603 // close the 'wrong' ends of the pipes in the parent
604 close(child_done_pipe[1]);
605 close(parent_done_pipe[0]);
606
607 // wait for the child to be done
608 read(child_done_pipe[0], &read_buf, 1);
609 close(child_done_pipe[0]);
610
611 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800612 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100613 childpipe[i] = parent_done_pipe[1];
614 }
615
Zhenhua WANG81aad002017-04-25 11:07:19 +0800616 // Sum the PSS of tested library of all the children
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100617 size_t total_pss = 0;
618 for (int i=0; i<CHILDREN; ++i) {
619 size_t child_pss;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800620 ASSERT_NO_FATAL_FAILURE(GetPss(share_relro, lib, relro_file, child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100621 total_pss += child_pss;
622 }
623 *pss_out = total_pss;
624
625 // Close pipes and wait for children to exit
626 for (int i=0; i<CHILDREN; ++i) {
627 ASSERT_NOERROR(close(childpipe[i]));
628 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800629 for (int i = 0; i < CHILDREN; ++i) {
630 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100631 }
632}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700633
634// Testing namespaces
635static const char* g_public_lib = "libnstest_public.so";
636
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800637// These are libs shared with default namespace
638static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
639
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700640TEST(dlext, ns_smoke) {
641 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800642 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700643
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800644 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
645 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
646 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
647 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700648
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800649 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800650 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700651 ASSERT_TRUE(handle_public != nullptr) << dlerror();
652
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800653 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700654
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800655 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700656 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800657 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800658 ASSERT_TRUE(handle_public == nullptr);
659 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
660 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
661
662 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700663
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800664 // create "public namespace", share limited set of public libraries with
665
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800666 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800667 android_create_namespace("private",
668 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800669 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800670 ANDROID_NAMESPACE_TYPE_REGULAR,
671 nullptr,
672 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700673 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800674 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700675
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800676 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800677 android_create_namespace("private_isolated",
678 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800679 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800680 ANDROID_NAMESPACE_TYPE_ISOLATED,
681 nullptr,
682 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700683 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800684 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700685
686 // This should not have affect search path for default namespace:
687 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
688 void* handle = dlopen(g_public_lib, RTLD_NOW);
689 ASSERT_TRUE(handle != nullptr) << dlerror();
690 dlclose(handle);
691
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700692 // dlopen for a public library using an absolute path should work
693 // 1. For isolated namespaces
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700694 android_dlextinfo extinfo;
695 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700696 extinfo.library_namespace = ns2;
697 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
698 ASSERT_TRUE(handle != nullptr) << dlerror();
699 ASSERT_TRUE(handle == handle_public);
700
701 dlclose(handle);
702
703 // 1.1 even if it wasn't loaded before
704 dlclose(handle_public);
705
706 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
707 ASSERT_TRUE(handle_public == nullptr);
708 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
709 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
710
711 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
712 ASSERT_TRUE(handle != nullptr) << dlerror();
713
714 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
715 ASSERT_TRUE(handle == handle_public);
716
717 dlclose(handle);
718
719 // 2. And for regular namespaces (make sure it does not load second copy of the library)
720 extinfo.library_namespace = ns1;
721 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
722 ASSERT_TRUE(handle != nullptr) << dlerror();
723 ASSERT_TRUE(handle == handle_public);
724
725 dlclose(handle);
726
727 // 2.1 Unless it was not loaded before - in which case it will load a duplicate.
728 // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces?
729 dlclose(handle_public);
730
731 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
732 ASSERT_TRUE(handle_public == nullptr);
733 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
734 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
735
736 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
737 ASSERT_TRUE(handle != nullptr) << dlerror();
738
739 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
740
741 ASSERT_TRUE(handle != handle_public);
742
743 dlclose(handle);
744
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700745 extinfo.library_namespace = ns1;
746
747 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
748 ASSERT_TRUE(handle1 != nullptr) << dlerror();
749
750 extinfo.library_namespace = ns2;
751 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
752 ASSERT_TRUE(handle2 != nullptr) << dlerror();
753
754 ASSERT_TRUE(handle1 != handle2);
755
756 typedef const char* (*fn_t)();
757
758 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
759 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
760 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
761 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
762
763 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
764 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
765
766 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
767
768 fn_t ns_get_private_extern_string1 =
769 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
770 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
771 fn_t ns_get_private_extern_string2 =
772 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
773 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
774
775 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
776 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
777
778 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
779
780 fn_t ns_get_public_extern_string1 =
781 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
782 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
783 fn_t ns_get_public_extern_string2 =
784 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
785 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
786
787 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
788 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
789
790 // and now check that dlopen() does the right thing in terms of preserving namespace
791 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
792 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
793 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
794 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
795
796 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
797 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
798
799 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
800
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800801 // Check that symbols from non-shared libraries a shared library depends on are not visible
802 // from original namespace.
803
804 fn_t ns_get_internal_extern_string =
805 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
806 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
807 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
808 "ns_get_internal_extern_string() expected to return null but returns \"" <<
809 ns_get_internal_extern_string() << "\"";
810
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700811 dlclose(handle1);
812
813 // Check if handle2 is still alive (and well)
814 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
815 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
816 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
817 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
818
819 dlclose(handle2);
820}
821
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700822TEST(dlext, dlopen_ext_use_o_tmpfile_fd) {
823 const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
824
825 int tmpfd = TEMP_FAILURE_RETRY(
826 open(get_testlib_root().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL));
827
828 // Ignore kernels without O_TMPFILE flag support
829 if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) {
830 return;
831 }
832
833 ASSERT_TRUE(tmpfd != -1) << strerror(errno);
834
835 android_namespace_t* ns =
836 android_create_namespace("testing-o_tmpfile",
837 nullptr,
838 get_testlib_root().c_str(),
839 ANDROID_NAMESPACE_TYPE_ISOLATED,
840 nullptr,
841 nullptr);
842
843 ASSERT_DL_NOTNULL(ns);
844
845 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
846
847 std::string content;
848 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
849 ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno);
850
851 android_dlextinfo extinfo;
852 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
853 extinfo.library_fd = tmpfd;
854 extinfo.library_namespace = ns;
855
856 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
857
858 ASSERT_DL_NOTNULL(handle);
859
860 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
861 ASSERT_DL_NOTNULL(taxicab_number);
862 EXPECT_EQ(1729U, *taxicab_number);
863 dlclose(handle);
864}
865
866TEST(dlext, dlopen_ext_use_memfd) {
867 const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
868
869 // create memfd
870 int memfd = syscall(__NR_memfd_create, "foobar", MFD_CLOEXEC);
871 if (memfd == -1 && errno == ENOSYS) {
872 return;
873 }
874
875 ASSERT_TRUE(memfd != -1) << strerror(errno);
876
877 // Check st.f_type is TMPFS_MAGIC for memfd
878 struct statfs st;
879 ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno);
880 ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type);
881
882 android_namespace_t* ns =
883 android_create_namespace("testing-memfd",
884 nullptr,
885 get_testlib_root().c_str(),
886 ANDROID_NAMESPACE_TYPE_ISOLATED,
887 nullptr,
888 nullptr);
889
890 ASSERT_DL_NOTNULL(ns);
891
892 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
893
894 // read file into memfd backed one.
895 std::string content;
896 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
897 ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno);
898
899 android_dlextinfo extinfo;
900 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
901 extinfo.library_fd = memfd;
902 extinfo.library_namespace = ns;
903
904 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
905
906 ASSERT_DL_NOTNULL(handle);
907
908 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
909 ASSERT_DL_NOTNULL(taxicab_number);
910 EXPECT_EQ(1729U, *taxicab_number);
911 dlclose(handle);
912}
913
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800914TEST(dlext, ns_symbol_visibilty_one_namespace) {
915 static const char* root_lib = "libnstest_root.so";
916 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
917
918 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs:" +
919 get_testlib_root() + "/private_namespace_libs";
920
921 android_namespace_t* ns =
922 android_create_namespace("one",
923 nullptr,
924 ns_search_path.c_str(),
925 ANDROID_NAMESPACE_TYPE_ISOLATED,
926 nullptr,
927 nullptr);
928
929 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
930
931 android_dlextinfo extinfo;
932 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
933 extinfo.library_namespace = ns;
934
935 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
936 ASSERT_TRUE(handle != nullptr) << dlerror();
937
938 typedef const char* (*fn_t)();
939
940 // Check that relocation worked correctly
941 fn_t ns_get_internal_extern_string =
942 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
943 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
944 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
945
946 fn_t internal_extern_string_fn =
947 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
948 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
949 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
950}
951
952TEST(dlext, ns_symbol_visibilty_between_namespaces) {
953 static const char* root_lib = "libnstest_root.so";
954 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
955
956 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
957 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
958
959 android_namespace_t* ns_public =
960 android_create_namespace("public",
961 nullptr,
962 public_ns_search_path.c_str(),
963 ANDROID_NAMESPACE_TYPE_ISOLATED,
964 nullptr,
965 nullptr);
966
967 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
968
969 android_namespace_t* ns_private =
970 android_create_namespace("private",
971 nullptr,
972 private_ns_search_path.c_str(),
973 ANDROID_NAMESPACE_TYPE_ISOLATED,
974 nullptr,
975 nullptr);
976
977 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
978 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
979
980 android_dlextinfo extinfo;
981 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
982 extinfo.library_namespace = ns_private;
983
984 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
985 ASSERT_TRUE(handle != nullptr) << dlerror();
986
987 typedef const char* (*fn_t)();
988
989 // Check that relocation worked correctly
990 fn_t ns_get_internal_extern_string =
991 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
992 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
993 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
994 "ns_get_internal_extern_string() expected to return null but returns \"" <<
995 ns_get_internal_extern_string() << "\"";
996
997 fn_t internal_extern_string_fn =
998 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
999 ASSERT_TRUE(internal_extern_string_fn == nullptr);
1000 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
1001}
1002
1003TEST(dlext, ns_unload_between_namespaces) {
1004 static const char* root_lib = "libnstest_root.so";
1005 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1006
1007 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
1008 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
1009
1010 android_namespace_t* ns_public =
1011 android_create_namespace("public",
1012 nullptr,
1013 public_ns_search_path.c_str(),
1014 ANDROID_NAMESPACE_TYPE_ISOLATED,
1015 nullptr,
1016 nullptr);
1017
1018 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1019
1020 android_namespace_t* ns_private =
1021 android_create_namespace("private",
1022 nullptr,
1023 private_ns_search_path.c_str(),
1024 ANDROID_NAMESPACE_TYPE_ISOLATED,
1025 nullptr,
1026 nullptr);
1027
1028 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
1029 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1030
1031 android_dlextinfo extinfo;
1032 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1033 extinfo.library_namespace = ns_private;
1034
1035 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1036 ASSERT_TRUE(handle != nullptr) << dlerror();
1037
1038 dlclose(handle);
1039 // Check that root_lib was unloaded
1040 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1041 ASSERT_TRUE(handle == nullptr);
1042 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
1043 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1044
1045 // Check that shared library was unloaded in public ns
1046 extinfo.library_namespace = ns_public;
1047 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1048 ASSERT_TRUE(handle == nullptr);
1049 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
1050 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1051}
1052
Dimitry Ivanov18623142017-02-21 13:41:08 -08001053TEST(dlext, ns_greylist) {
1054 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1055
1056 const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
1057
1058 android_namespace_t* ns =
1059 android_create_namespace("namespace",
1060 nullptr,
1061 ns_search_path.c_str(),
1062 ANDROID_NAMESPACE_TYPE_ISOLATED,
1063 nullptr,
1064 nullptr);
1065
1066 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1067
1068 android_dlextinfo extinfo;
1069 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1070 extinfo.library_namespace = ns;
1071
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001072 // An app targeting M can open libnativehelper.so because it's on the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001073 android_set_application_target_sdk_version(__ANDROID_API_M__);
1074 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1075 ASSERT_TRUE(handle != nullptr) << dlerror();
1076
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001077 // Check that loader did not load another copy of libdl.so while loading greylisted library.
1078 void* dlsym_ptr = dlsym(handle, "dlsym");
1079 ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror();
1080 ASSERT_EQ(&dlsym, dlsym_ptr);
1081
Dimitry Ivanov18623142017-02-21 13:41:08 -08001082 dlclose(handle);
1083
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001084 // An app targeting N no longer has the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001085 android_set_application_target_sdk_version(__ANDROID_API_N__);
1086 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1087 ASSERT_TRUE(handle == nullptr);
1088 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1089}
1090
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001091TEST(dlext, ns_cyclic_namespaces) {
1092 // Test that ns1->ns2->ns1 link does not break the loader
1093 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1094 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
1095
1096 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs";
1097
1098 android_namespace_t* ns1 =
1099 android_create_namespace("ns1",
1100 nullptr,
1101 ns_search_path.c_str(),
1102 ANDROID_NAMESPACE_TYPE_ISOLATED,
1103 nullptr,
1104 nullptr);
1105
1106 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
1107
1108 android_namespace_t* ns2 =
1109 android_create_namespace("ns1",
1110 nullptr,
1111 ns_search_path.c_str(),
1112 ANDROID_NAMESPACE_TYPE_ISOLATED,
1113 nullptr,
1114 nullptr);
1115
1116 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
1117
1118 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
1119 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
1120
1121 android_dlextinfo extinfo;
1122 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1123 extinfo.library_namespace = ns1;
1124
1125 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
1126 ASSERT_TRUE(handle == nullptr);
1127 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
1128}
1129
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001130TEST(dlext, ns_isolated) {
1131 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001132 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001133
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001134 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001135 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001136 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1137
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08001138 android_set_application_target_sdk_version(42U); // something > 23
1139
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001140 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001141
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001142 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001143 android_create_namespace("private",
1144 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001145 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001146 ANDROID_NAMESPACE_TYPE_REGULAR,
1147 nullptr,
1148 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001149 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001150 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001151
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001152 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001153 android_create_namespace("private_isolated1",
1154 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001155 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001156 ANDROID_NAMESPACE_TYPE_ISOLATED,
1157 nullptr,
1158 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001159 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001160 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001161
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001162 android_namespace_t* ns_isolated2 =
1163 android_create_namespace("private_isolated2",
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001164 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001165 nullptr,
1166 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001167 get_testlib_root().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001168 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001169 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001170 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001171
1172 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1173 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1174
1175 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001176 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001177
1178 // Load lib_private_external_path to default namespace
1179 // (it should remain invisible for the isolated namespaces after this)
1180 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1181 ASSERT_TRUE(handle != nullptr) << dlerror();
1182
1183 android_dlextinfo extinfo;
1184 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1185 extinfo.library_namespace = ns_not_isolated;
1186
1187 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1188 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1189
1190 extinfo.library_namespace = ns_isolated;
1191
1192 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1193 ASSERT_TRUE(handle2 == nullptr);
1194 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1195
1196 // Check dlopen by absolute path
1197 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1198 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001199 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001200 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001201 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001202
1203 extinfo.library_namespace = ns_isolated2;
1204
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001205 // this should work because isolation_path for private_isolated2 includes get_testlib_root()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001206 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001207 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1208 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001209
1210 // Check dlopen by absolute path
1211 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001212 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1213 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001214
1215 typedef const char* (*fn_t)();
1216 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1217 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1218
1219 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1220
1221 fn_t ns_get_private_extern_string =
1222 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1223 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1224
1225 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1226
1227 fn_t ns_get_public_extern_string =
1228 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1229 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1230
1231 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1232
1233 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1234 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1235
1236 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1237
1238 dlclose(handle1);
1239}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001240
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001241TEST(dlext, ns_shared) {
1242 static const char* root_lib = "libnstest_root_not_isolated.so";
1243 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001244
1245 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001246
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001247 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001248 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1249 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1250
1251 android_set_application_target_sdk_version(42U); // something > 23
1252
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001253 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001254
1255 // preload this library to the default namespace to check if it
1256 // is shared later on.
1257 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001258 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001259 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1260
1261 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001262 android_create_namespace("private",
1263 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001264 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001265 ANDROID_NAMESPACE_TYPE_REGULAR,
1266 nullptr,
1267 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001268 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001269 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001270
1271 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001272 android_create_namespace("private_isolated_shared",
1273 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001274 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001275 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001276 nullptr,
1277 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001278 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001279 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001280
1281 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1282 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1283
1284 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001285 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001286
1287 // Load lib_private_external_path to default namespace
1288 // (it should remain invisible for the isolated namespaces after this)
1289 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1290 ASSERT_TRUE(handle != nullptr) << dlerror();
1291
1292 android_dlextinfo extinfo;
1293 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1294 extinfo.library_namespace = ns_not_isolated;
1295
1296 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1297 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1298
1299 extinfo.library_namespace = ns_isolated_shared;
1300
1301 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1302 ASSERT_TRUE(handle2 == nullptr);
1303 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1304
1305 // Check dlopen by absolute path
1306 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1307 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001308 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001309 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001310 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001311
1312 // load libnstest_root.so to shared namespace in order to check that everything is different
1313 // except shared libnstest_dlopened.so
1314
1315 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1316
1317 typedef const char* (*fn_t)();
1318 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1319 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1320 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1321 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1322
1323 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1324 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1325 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1326
1327 fn_t ns_get_private_extern_string =
1328 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1329 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1330 fn_t ns_get_private_extern_string_shared =
1331 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1332 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1333
1334 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1335 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1336 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1337
1338 fn_t ns_get_public_extern_string =
1339 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1340 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1341 fn_t ns_get_public_extern_string_shared =
1342 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1343 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1344
1345 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1346 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1347 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1348
1349 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1350 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1351 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1352 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1353 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1354 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1355
1356 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1357 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1358 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1359 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1360 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1361
1362 dlclose(handle1);
1363 dlclose(handle2);
1364}
1365
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001366TEST(dlext, ns_shared_links_and_paths) {
1367 // Create parent namespace (isolated, not shared)
1368 android_namespace_t* ns_isolated =
1369 android_create_namespace("private_isolated",
1370 nullptr,
1371 (get_testlib_root() + "/private_namespace_libs").c_str(),
1372 ANDROID_NAMESPACE_TYPE_ISOLATED,
1373 (get_testlib_root() + "/public_namespace_libs").c_str(),
1374 nullptr);
1375 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
1376 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
1377
1378 // Create shared namespace with ns_isolated parent
1379 android_namespace_t* ns_shared =
1380 android_create_namespace("private_shared",
1381 nullptr,
1382 nullptr,
1383 ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED,
1384 nullptr,
1385 ns_isolated);
1386 ASSERT_TRUE(ns_shared != nullptr) << dlerror();
1387
1388 // 1. Load a library in ns_shared to check that it has inherited
1389 // search path and the link to the default namespace.
1390 android_dlextinfo extinfo;
1391 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1392 extinfo.library_namespace = ns_shared;
1393
1394 {
1395 void* handle = android_dlopen_ext("libnstest_private.so", RTLD_NOW, &extinfo);
1396 ASSERT_TRUE(handle != nullptr) << dlerror();
1397 const char** ns_private_extern_string = static_cast<const char**>(dlsym(handle, "g_private_extern_string"));
1398 ASSERT_TRUE(ns_private_extern_string != nullptr) << dlerror();
1399 ASSERT_STREQ("This string is from private namespace", *ns_private_extern_string);
1400
1401 dlclose(handle);
1402 }
1403 // 2. Load another test library by absolute path to check that
1404 // it has inherited permitted_when_isolated_path
1405 {
1406 void* handle = android_dlopen_ext(
1407 (get_testlib_root() + "/public_namespace_libs/libnstest_public.so").c_str(),
1408 RTLD_NOW,
1409 &extinfo);
1410
1411 ASSERT_TRUE(handle != nullptr) << dlerror();
1412 const char** ns_public_extern_string = static_cast<const char**>(dlsym(handle, "g_public_extern_string"));
1413 ASSERT_TRUE(ns_public_extern_string != nullptr) << dlerror();
1414 ASSERT_STREQ("This string is from public namespace", *ns_public_extern_string);
1415
1416 dlclose(handle);
1417 }
1418
1419 // 3. Check that it is still isolated.
1420 {
1421 void* handle = android_dlopen_ext(
1422 (get_testlib_root() + "/libtest_empty.so").c_str(),
1423 RTLD_NOW,
1424 &extinfo);
1425
1426 ASSERT_TRUE(handle == nullptr);
1427 }
1428}
1429
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001430TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001431 android_set_application_target_sdk_version(42U); // something > 23
1432
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001433 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001434
1435 // preload this library to the default namespace to check if it
1436 // is shared later on.
1437 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001438 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001439 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1440
1441 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001442 android_create_namespace("private_isolated_shared",
1443 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001444 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001445 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001446 nullptr,
1447 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001448 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001449 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001450
1451 // Check if "libnstest_dlopened.so" is loaded (and the same)
1452 android_dlextinfo extinfo;
1453 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1454 extinfo.library_namespace = ns_isolated_shared;
1455
1456 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1457 ASSERT_TRUE(handle != nullptr) << dlerror();
1458 ASSERT_TRUE(handle == handle_dlopened);
1459 dlclose(handle);
1460 dlclose(handle_dlopened);
1461
1462 // And now check that the library cannot be found by soname (and is no longer loaded)
1463 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1464 ASSERT_TRUE(handle == nullptr)
1465 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1466
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001467 handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001468 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1469 ASSERT_TRUE(handle == nullptr)
1470 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1471
1472 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1473 ASSERT_TRUE(handle == nullptr)
1474 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1475
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001476 handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001477 RTLD_NOW | RTLD_NOLOAD);
1478 ASSERT_TRUE(handle == nullptr)
1479 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1480
1481 // Now lets see if the soinfo area gets reused in the wrong way:
1482 // load a library to default namespace.
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001483 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001484 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1485 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1486
1487 // try to find it in shared namespace
1488 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1489 ASSERT_TRUE(handle == nullptr)
1490 << "Error: " << g_public_lib << " is accessible in shared namespace";
1491}
1492
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001493TEST(dlext, ns_isolated_rtld_global) {
1494 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001495 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001496
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001497 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001498
1499 android_namespace_t* ns1 =
1500 android_create_namespace("isolated1",
1501 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001502 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001503 ANDROID_NAMESPACE_TYPE_ISOLATED,
1504 lib_public_path.c_str(),
1505 nullptr);
1506 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001507 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001508
1509 android_namespace_t* ns2 =
1510 android_create_namespace("isolated2",
1511 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001512 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001513 ANDROID_NAMESPACE_TYPE_ISOLATED,
1514 lib_public_path.c_str(),
1515 nullptr);
1516 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001517 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001518
1519 android_dlextinfo extinfo;
1520 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1521 extinfo.library_namespace = ns1;
1522
1523 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1524 RTLD_GLOBAL,
1525 &extinfo);
1526
1527 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1528
1529 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001530 android_create_namespace("isolated1_child",
1531 nullptr,
1532 (get_testlib_root() + "/private_namespace_libs").c_str(),
1533 ANDROID_NAMESPACE_TYPE_ISOLATED,
1534 nullptr,
1535 ns1);
1536
1537 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1538 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001539
1540 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1541 // attempt to use ns2 should result in dlerror()
1542
1543 // Check ns1_child first.
1544 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1545 extinfo.library_namespace = ns1_child;
1546
1547 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1548 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1549
1550 // now ns1
1551 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1552 extinfo.library_namespace = ns1;
1553
1554 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1555 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1556
1557 // and ns2 should fail
1558 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1559 extinfo.library_namespace = ns2;
1560
1561 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1562 ASSERT_TRUE(handle1 == nullptr);
1563 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1564}
1565
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001566TEST(dlext, ns_anonymous) {
1567 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001568 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001569
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001570 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001571 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1572
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001573 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1574
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001575 ASSERT_TRUE(
1576 android_init_anonymous_namespace(shared_libs.c_str(),
1577 (get_testlib_root() + "/private_namespace_libs").c_str())
1578 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001579
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001580 android_namespace_t* ns =
1581 android_create_namespace("private",
1582 nullptr,
1583 (get_testlib_root() + "/private_namespace_libs").c_str(),
1584 ANDROID_NAMESPACE_TYPE_REGULAR,
1585 nullptr,
1586 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001587
1588 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001589 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001590
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001591 std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001592
1593 android_dlextinfo extinfo;
1594 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1595 extinfo.library_namespace = ns;
1596
1597 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1598 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1599 ASSERT_TRUE(handle != nullptr) << dlerror();
1600
1601 uintptr_t ns_get_dlopened_string_addr =
1602 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1603 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1604 typedef const char* (*fn_t)();
1605 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1606
1607 std::vector<map_record> maps;
1608 Maps::parse_maps(&maps);
1609
1610 uintptr_t addr_start = 0;
1611 uintptr_t addr_end = 0;
1612 std::vector<map_record> maps_to_copy;
1613
1614 for (const auto& rec : maps) {
1615 if (rec.pathname == private_library_absolute_path) {
1616 if (addr_start == 0) {
1617 addr_start = rec.addr_start;
1618 }
1619 addr_end = rec.addr_end;
1620
1621 maps_to_copy.push_back(rec);
1622 }
1623 }
1624
1625 // some sanity checks..
1626 ASSERT_TRUE(addr_start > 0);
1627 ASSERT_TRUE(addr_end > 0);
1628 ASSERT_EQ(3U, maps_to_copy.size());
1629 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1630 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1631
1632 // copy
1633 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1634 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1635 -1, 0));
1636 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1637
1638 for (const auto& rec : maps_to_copy) {
1639 uintptr_t offset = rec.addr_start - addr_start;
1640 size_t size = rec.addr_end - rec.addr_start;
1641 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1642 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1643 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1644 ASSERT_TRUE(map != MAP_FAILED);
1645 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1646 mprotect(map, size, rec.perms);
1647 }
1648
1649 // call the function copy
1650 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1651 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1652 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1653 ns_get_dlopened_string_anon());
1654
1655 // They should belong to different namespaces (private and anonymous)
1656 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1657 ns_get_dlopened_string_private());
1658
1659 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1660}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001661
1662TEST(dlext, dlopen_handle_value_platform) {
1663 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1664 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1665 << "dlopen should return odd value for the handle";
1666 dlclose(handle);
1667}
1668
1669TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001670 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001671 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1672 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1673 << "dlopen should return valid pointer";
1674 dlclose(handle);
1675}