blob: 3f6da59ca320f2724606c3513937940cd507eaea [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>
Elliott Hughesd7c52622017-06-20 14:26:56 -070033#include <sys/syscall.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010034#include <sys/types.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070035#include <sys/vfs.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000036#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000037
Torne (Richard Coles)26052612014-05-02 14:57:42 +010038#include <pagemap/pagemap.h>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080039#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010040
Dimitry Ivanov927877c2016-09-21 11:17:13 -070041#include "gtest_globals.h"
Yabin Cui294d1e22014-12-07 20:43:37 -080042#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080043#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070044#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070045#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000046
47#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070048 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000049
50#define ASSERT_DL_ZERO(i) \
51 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
52
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000053#define ASSERT_NOERROR(i) \
54 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
55
Yabin Cui16f7f8d2014-11-04 11:08:05 -080056#define ASSERT_SUBSTR(needle, haystack) \
57 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
58
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000059
60typedef int (*fn)(void);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070061constexpr const char* kLibName = "libdlext_test.so";
62constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
63constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
64constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070065
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000066class DlExtTest : public ::testing::Test {
67protected:
68 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070069 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000070 // verify that we don't have the library loaded already
Dimitry Ivanov927877c2016-09-21 11:17:13 -070071 void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070072 ASSERT_TRUE(h == nullptr);
Dimitry Ivanov927877c2016-09-21 11:17:13 -070073 h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070074 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000075 // call dlerror() to swallow the error, and check it was the one we wanted
Dimitry Ivanov927877c2016-09-21 11:17:13 -070076 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 +000077 }
78
79 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070080 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000081 ASSERT_DL_ZERO(dlclose(handle_));
82 }
83 }
84
85 void* handle_;
86};
87
88TEST_F(DlExtTest, ExtInfoNull) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -070089 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000090 ASSERT_DL_NOTNULL(handle_);
91 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
92 ASSERT_DL_NOTNULL(f);
93 EXPECT_EQ(4, f());
94}
95
96TEST_F(DlExtTest, ExtInfoNoFlags) {
97 android_dlextinfo extinfo;
98 extinfo.flags = 0;
Dimitry Ivanov927877c2016-09-21 11:17:13 -070099 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000100 ASSERT_DL_NOTNULL(handle_);
101 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
102 ASSERT_DL_NOTNULL(f);
103 EXPECT_EQ(4, f());
104}
105
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700106TEST_F(DlExtTest, ExtInfoUseFd) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800107 const std::string lib_path = get_testlib_root() + "/libdlext_test_fd/libdlext_test_fd.so";
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108
109 android_dlextinfo extinfo;
110 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700111 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700112 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700113 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700114 ASSERT_DL_NOTNULL(handle_);
115 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
116 ASSERT_DL_NOTNULL(f);
117 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700118
119 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
120 ASSERT_DL_NOTNULL(taxicab_number);
121 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700122}
123
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700124TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800125 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700126
127 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700128 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700129 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800130
131 // Find the offset of the shared library in the zip.
132 ZipArchiveHandle handle;
133 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
134 ZipEntry zip_entry;
135 ZipString zip_name;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700136 zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
137 zip_name.name_length = strlen(kLibZipSimpleZip);
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800138 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
139 extinfo.library_fd_offset = zip_entry.offset;
140 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700141
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700142 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700143 ASSERT_DL_NOTNULL(handle_);
144
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700145 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
146 ASSERT_DL_NOTNULL(taxicab_number);
147 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700148}
149
150TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800151 const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700152
153 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700154 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700155 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700156 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700157
158 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
159 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700160 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
161
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800162 // Test an address above 2^44, for http://b/18178121 .
163 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700164 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700165 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800166 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
167
168 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
169 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
170 ASSERT_TRUE(handle_ == nullptr);
171 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
172
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700173 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700174 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800175 ASSERT_TRUE(handle_ == nullptr);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700176 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700177
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800178 // Check if dlsym works after unsuccessful dlopen().
179 // Supply non-exiting one to make linker visit every soinfo.
180 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
181 ASSERT_TRUE(sym == nullptr);
182
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700183 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700184}
185
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800186TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700187 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700188 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800189 // This offset will not be used, so it doesn't matter.
190 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700191
192 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
193 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700194 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 -0700195}
196
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700197TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700198 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
199 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700200 // 1. Open actual file
201 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
202 ASSERT_DL_NOTNULL(handle);
203 // 2. Open link with force_load flag set
204 android_dlextinfo extinfo;
205 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700206 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700207 ASSERT_DL_NOTNULL(handle2);
208 ASSERT_TRUE(handle != handle2);
209
210 dlclose(handle2);
211 dlclose(handle);
212}
213
214TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700215 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
216 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700217 // Check if soname lookup still returns already loaded library
218 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700219 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700220 ASSERT_DL_NOTNULL(handle);
221
222 android_dlextinfo extinfo;
223 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
224
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700225 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700226 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
227
228 ASSERT_DL_NOTNULL(handle2);
229 ASSERT_TRUE(handle == handle2);
230
231 dlclose(handle2);
232 dlclose(handle);
233}
234
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700235TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700236 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800237 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700238
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700239 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700240 ASSERT_TRUE(handle != nullptr) << dlerror();
241
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700242 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
243 ASSERT_DL_NOTNULL(taxicab_number);
244 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700245
246 dlclose(handle);
247}
248
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700249TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700250 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800251 const std::string lib_path = get_testlib_root() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700252
253 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
254
255 ASSERT_TRUE(handle != nullptr) << dlerror();
256
257 typedef void *(* dlopen_b_fn)();
258 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
259 ASSERT_TRUE(fn != nullptr) << dlerror();
260
261 void *p = fn();
262 ASSERT_TRUE(p != nullptr) << dlerror();
263
264 dlclose(p);
265 dlclose(handle);
266}
267
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700268TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700269 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800270 const std::string lib_path = get_testlib_root() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700271
272 typedef void (*fn_t)(const char*);
273 fn_t android_update_LD_LIBRARY_PATH =
274 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
275
276 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
277
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700278 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700279 ASSERT_TRUE(handle == nullptr);
280
281 android_update_LD_LIBRARY_PATH(lib_path.c_str());
282
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700283 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700284 ASSERT_TRUE(handle != nullptr) << dlerror();
285
286 int (*fn)(void);
287 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
288 ASSERT_TRUE(fn != nullptr);
289 EXPECT_EQ(4, fn());
290
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800291 uint32_t* taxicab_number =
292 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700293 ASSERT_DL_NOTNULL(taxicab_number);
294 EXPECT_EQ(1729U, *taxicab_number);
295
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700296 dlclose(handle);
297}
298
299
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000300TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700301 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000302 ASSERT_TRUE(start != MAP_FAILED);
303 android_dlextinfo extinfo;
304 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
305 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700306 extinfo.reserved_size = kLibSize;
307 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000308 ASSERT_DL_NOTNULL(handle_);
309 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
310 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700311 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000312 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700313 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000314 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800315
316 // Check that after dlclose reserved address space is unmapped (and can be reused)
317 dlclose(handle_);
318 handle_ = nullptr;
319
320 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
321 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000322}
323
324TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800325 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000326 ASSERT_TRUE(start != MAP_FAILED);
327 android_dlextinfo extinfo;
328 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
329 extinfo.reserved_addr = start;
330 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700331 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700332 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000333}
334
335TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700336 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000337 ASSERT_TRUE(start != MAP_FAILED);
338 android_dlextinfo extinfo;
339 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
340 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700341 extinfo.reserved_size = kLibSize;
342 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000343 ASSERT_DL_NOTNULL(handle_);
344 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
345 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700346 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000347 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700348 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000349 EXPECT_EQ(4, f());
350}
351
352TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800353 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000354 ASSERT_TRUE(start != MAP_FAILED);
355 android_dlextinfo extinfo;
356 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
357 extinfo.reserved_addr = start;
358 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700359 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000360 ASSERT_DL_NOTNULL(handle_);
361 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
362 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700363 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
364 (reinterpret_cast<void*>(f) >=
365 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000366 EXPECT_EQ(4, f());
367}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000368
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700369TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700370 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700371 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700372 munmap(start, kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700373
374 android_dlextinfo extinfo;
375 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
376 extinfo.reserved_addr = start;
377
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700378 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700379 ASSERT_DL_NOTNULL(handle_);
380 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
381 ASSERT_DL_NOTNULL(f);
382 EXPECT_GE(reinterpret_cast<void*>(f), start);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700383 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700384
385 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800386 dlclose(handle_);
387 handle_ = nullptr;
388
389 // Check that dlclose unmapped the file
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700390 void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800391 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700392}
393
394TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700395 void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700396 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
397 ASSERT_TRUE(start != MAP_FAILED);
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700398 munmap(start, kLibSize + PAGE_SIZE);
399 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700400 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
401 ASSERT_TRUE(new_addr != MAP_FAILED);
402
403 android_dlextinfo extinfo;
404 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
405 extinfo.reserved_addr = start;
406
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700407 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700408 ASSERT_TRUE(handle_ == nullptr);
409}
410
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100411class DlExtRelroSharingTest : public DlExtTest {
412protected:
413 virtual void SetUp() {
414 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700415 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100416 ASSERT_TRUE(start != MAP_FAILED);
417 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
418 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700419 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100420 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000421 }
422
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100423 virtual void TearDown() {
424 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100425 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000426
Yabin Cui294d1e22014-12-07 20:43:37 -0800427 void CreateRelroFile(const char* lib, const char* relro_file) {
428 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100429 ASSERT_NOERROR(relro_fd);
430
431 pid_t pid = fork();
432 if (pid == 0) {
433 // child process
434 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
435 extinfo_.relro_fd = relro_fd;
436 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700437 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100438 fprintf(stderr, "in child: %s\n", dlerror());
439 exit(1);
440 }
441 exit(0);
442 }
443
444 // continuing in parent
445 ASSERT_NOERROR(close(relro_fd));
446 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800447 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100448
449 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800450 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100451 ASSERT_NOERROR(relro_fd);
452 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
453 extinfo_.relro_fd = relro_fd;
454 }
455
456 void TryUsingRelro(const char* lib) {
457 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
458 ASSERT_DL_NOTNULL(handle_);
459 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
460 ASSERT_DL_NOTNULL(f);
461 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700462
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800463 uint32_t* taxicab_number =
464 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700465 ASSERT_DL_NOTNULL(taxicab_number);
466 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100467 }
468
Zhenhua WANG81aad002017-04-25 11:07:19 +0800469 void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro,
470 size_t* pss_out);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100471
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100472 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100473};
474
475TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800476 TemporaryFile tf; // Use tf to get an unique filename.
477 ASSERT_NOERROR(close(tf.fd));
478
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700479 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
480 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Yabin Cui294d1e22014-12-07 20:43:37 -0800481
482 // Use destructor of tf to close and unlink the file.
483 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100484}
485
486TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800487 TemporaryFile tf; // // Use tf to get an unique filename.
488 ASSERT_NOERROR(close(tf.fd));
489
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700490 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
491 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
Yabin Cui294d1e22014-12-07 20:43:37 -0800492
493 // Use destructor of tf to close and unlink the file.
494 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100495}
496
497TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700498 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000499}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100500
501TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700502 if (geteuid() != 0) {
503 GTEST_LOG_(INFO) << "This test must be run as root.\n";
504 return;
505 }
506
Yabin Cui294d1e22014-12-07 20:43:37 -0800507 TemporaryFile tf; // Use tf to get an unique filename.
508 ASSERT_NOERROR(close(tf.fd));
509
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700510 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
Yabin Cui294d1e22014-12-07 20:43:37 -0800511
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100512 int pipefd[2];
513 ASSERT_NOERROR(pipe(pipefd));
514
515 size_t without_sharing, with_sharing;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800516 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, false, &without_sharing));
517 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, true, &with_sharing));
518 ASSERT_LT(with_sharing, without_sharing);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100519
Zhenhua WANG81aad002017-04-25 11:07:19 +0800520 // We expect the sharing to save at least 50% of the library's total PSS.
521 // In practice it saves 80%+ for this library in the test.
522 size_t pss_saved = without_sharing - with_sharing;
523 size_t expected_min_saved = without_sharing / 2;
524
525 EXPECT_LT(expected_min_saved, pss_saved);
Yabin Cui294d1e22014-12-07 20:43:37 -0800526
527 // Use destructor of tf to close and unlink the file.
528 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100529}
530
Zhenhua WANG81aad002017-04-25 11:07:19 +0800531void GetPss(bool shared_relro, const char* lib, const char* relro_file, pid_t pid,
532 size_t* total_pss) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100533 pm_kernel_t* kernel;
534 ASSERT_EQ(0, pm_kernel_create(&kernel));
535
536 pm_process_t* process;
537 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
538
539 pm_map_t** maps;
540 size_t num_maps;
541 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
542
Zhenhua WANG81aad002017-04-25 11:07:19 +0800543 // Calculate total PSS of the library.
544 *total_pss = 0;
545 bool saw_relro_file = false;
546 for (size_t i = 0; i < num_maps; ++i) {
547 if (android::base::EndsWith(maps[i]->name, lib) || strcmp(maps[i]->name, relro_file) == 0) {
548 if (strcmp(maps[i]->name, relro_file) == 0) saw_relro_file = true;
549
550 pm_memusage_t usage;
551 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
552 *total_pss += usage.pss;
553 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100554 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100555
556 free(maps);
557 pm_process_destroy(process);
558 pm_kernel_destroy(kernel);
Zhenhua WANG81aad002017-04-25 11:07:19 +0800559
560 if (shared_relro) ASSERT_TRUE(saw_relro_file);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100561}
562
Zhenhua WANG81aad002017-04-25 11:07:19 +0800563void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file,
564 bool share_relro, size_t* pss_out) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100565 const int CHILDREN = 20;
566
567 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800568 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100569 int childpipe[CHILDREN];
570 for (int i=0; i<CHILDREN; ++i) {
571 char read_buf;
572 int child_done_pipe[2], parent_done_pipe[2];
573 ASSERT_NOERROR(pipe(child_done_pipe));
574 ASSERT_NOERROR(pipe(parent_done_pipe));
575
576 pid_t child = fork();
577 if (child == 0) {
578 // close the 'wrong' ends of the pipes in the child
579 close(child_done_pipe[0]);
580 close(parent_done_pipe[1]);
581
582 // open the library
583 void* handle;
584 if (share_relro) {
585 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
586 } else {
587 handle = dlopen(lib, RTLD_NOW);
588 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700589 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100590 fprintf(stderr, "in child: %s\n", dlerror());
591 exit(1);
592 }
593
594 // close write end of child_done_pipe to signal the parent that we're done.
595 close(child_done_pipe[1]);
596
597 // wait for the parent to close parent_done_pipe, then exit
598 read(parent_done_pipe[0], &read_buf, 1);
599 exit(0);
600 }
601
602 ASSERT_NOERROR(child);
603
604 // close the 'wrong' ends of the pipes in the parent
605 close(child_done_pipe[1]);
606 close(parent_done_pipe[0]);
607
608 // wait for the child to be done
609 read(child_done_pipe[0], &read_buf, 1);
610 close(child_done_pipe[0]);
611
612 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800613 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100614 childpipe[i] = parent_done_pipe[1];
615 }
616
Zhenhua WANG81aad002017-04-25 11:07:19 +0800617 // Sum the PSS of tested library of all the children
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100618 size_t total_pss = 0;
619 for (int i=0; i<CHILDREN; ++i) {
620 size_t child_pss;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800621 ASSERT_NO_FATAL_FAILURE(GetPss(share_relro, lib, relro_file, child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100622 total_pss += child_pss;
623 }
624 *pss_out = total_pss;
625
626 // Close pipes and wait for children to exit
627 for (int i=0; i<CHILDREN; ++i) {
628 ASSERT_NOERROR(close(childpipe[i]));
629 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800630 for (int i = 0; i < CHILDREN; ++i) {
631 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100632 }
633}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700634
635// Testing namespaces
636static const char* g_public_lib = "libnstest_public.so";
637
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800638// These are libs shared with default namespace
639static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
640
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700641TEST(dlext, ns_smoke) {
642 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800643 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700644
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800645 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
646 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
647 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
648 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700649
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800650 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800651 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700652 ASSERT_TRUE(handle_public != nullptr) << dlerror();
653
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800654 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700655
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800656 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700657 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800658 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800659 ASSERT_TRUE(handle_public == nullptr);
660 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
661 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
662
663 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700664
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800665 // create "public namespace", share limited set of public libraries with
666
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800667 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800668 android_create_namespace("private",
669 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800670 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800671 ANDROID_NAMESPACE_TYPE_REGULAR,
672 nullptr,
673 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700674 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800675 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700676
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800677 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800678 android_create_namespace("private_isolated",
679 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800680 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800681 ANDROID_NAMESPACE_TYPE_ISOLATED,
682 nullptr,
683 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700684 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800685 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700686
687 // This should not have affect search path for default namespace:
688 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
689 void* handle = dlopen(g_public_lib, RTLD_NOW);
690 ASSERT_TRUE(handle != nullptr) << dlerror();
691 dlclose(handle);
692
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700693 // dlopen for a public library using an absolute path should work
694 // 1. For isolated namespaces
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700695 android_dlextinfo extinfo;
696 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700697 extinfo.library_namespace = ns2;
698 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
699 ASSERT_TRUE(handle != nullptr) << dlerror();
700 ASSERT_TRUE(handle == handle_public);
701
702 dlclose(handle);
703
704 // 1.1 even if it wasn't loaded before
705 dlclose(handle_public);
706
707 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
708 ASSERT_TRUE(handle_public == nullptr);
709 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
710 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
711
712 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
713 ASSERT_TRUE(handle != nullptr) << dlerror();
714
715 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
716 ASSERT_TRUE(handle == handle_public);
717
718 dlclose(handle);
719
720 // 2. And for regular namespaces (make sure it does not load second copy of the library)
721 extinfo.library_namespace = ns1;
722 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
723 ASSERT_TRUE(handle != nullptr) << dlerror();
724 ASSERT_TRUE(handle == handle_public);
725
726 dlclose(handle);
727
728 // 2.1 Unless it was not loaded before - in which case it will load a duplicate.
729 // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces?
730 dlclose(handle_public);
731
732 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
733 ASSERT_TRUE(handle_public == nullptr);
734 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
735 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
736
737 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
738 ASSERT_TRUE(handle != nullptr) << dlerror();
739
740 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
741
742 ASSERT_TRUE(handle != handle_public);
743
744 dlclose(handle);
745
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700746 extinfo.library_namespace = ns1;
747
748 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
749 ASSERT_TRUE(handle1 != nullptr) << dlerror();
750
751 extinfo.library_namespace = ns2;
752 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
753 ASSERT_TRUE(handle2 != nullptr) << dlerror();
754
755 ASSERT_TRUE(handle1 != handle2);
756
757 typedef const char* (*fn_t)();
758
759 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
760 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
761 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
762 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
763
764 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
765 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
766
767 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
768
769 fn_t ns_get_private_extern_string1 =
770 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
771 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
772 fn_t ns_get_private_extern_string2 =
773 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
774 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
775
776 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
777 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
778
779 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
780
781 fn_t ns_get_public_extern_string1 =
782 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
783 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
784 fn_t ns_get_public_extern_string2 =
785 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
786 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
787
788 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
789 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
790
791 // and now check that dlopen() does the right thing in terms of preserving namespace
792 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
793 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
794 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
795 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
796
797 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
798 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
799
800 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
801
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800802 // Check that symbols from non-shared libraries a shared library depends on are not visible
803 // from original namespace.
804
805 fn_t ns_get_internal_extern_string =
806 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
807 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
808 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
809 "ns_get_internal_extern_string() expected to return null but returns \"" <<
810 ns_get_internal_extern_string() << "\"";
811
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700812 dlclose(handle1);
813
814 // Check if handle2 is still alive (and well)
815 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
816 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
817 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
818 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
819
820 dlclose(handle2);
821}
822
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700823TEST(dlext, dlopen_ext_use_o_tmpfile_fd) {
824 const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
825
826 int tmpfd = TEMP_FAILURE_RETRY(
Elliott Hughesb115aef2017-08-04 09:34:19 -0700827 open(get_testlib_root().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL, 0));
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700828
829 // Ignore kernels without O_TMPFILE flag support
830 if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) {
831 return;
832 }
833
834 ASSERT_TRUE(tmpfd != -1) << strerror(errno);
835
836 android_namespace_t* ns =
837 android_create_namespace("testing-o_tmpfile",
838 nullptr,
839 get_testlib_root().c_str(),
840 ANDROID_NAMESPACE_TYPE_ISOLATED,
841 nullptr,
842 nullptr);
843
844 ASSERT_DL_NOTNULL(ns);
845
846 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
847
848 std::string content;
849 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
850 ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno);
851
852 android_dlextinfo extinfo;
853 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
854 extinfo.library_fd = tmpfd;
855 extinfo.library_namespace = ns;
856
857 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
858
859 ASSERT_DL_NOTNULL(handle);
860
861 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
862 ASSERT_DL_NOTNULL(taxicab_number);
863 EXPECT_EQ(1729U, *taxicab_number);
864 dlclose(handle);
865}
866
867TEST(dlext, dlopen_ext_use_memfd) {
868 const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
869
870 // create memfd
871 int memfd = syscall(__NR_memfd_create, "foobar", MFD_CLOEXEC);
872 if (memfd == -1 && errno == ENOSYS) {
873 return;
874 }
875
876 ASSERT_TRUE(memfd != -1) << strerror(errno);
877
878 // Check st.f_type is TMPFS_MAGIC for memfd
879 struct statfs st;
880 ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno);
881 ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type);
882
883 android_namespace_t* ns =
884 android_create_namespace("testing-memfd",
885 nullptr,
886 get_testlib_root().c_str(),
887 ANDROID_NAMESPACE_TYPE_ISOLATED,
888 nullptr,
889 nullptr);
890
891 ASSERT_DL_NOTNULL(ns);
892
893 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
894
895 // read file into memfd backed one.
896 std::string content;
897 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
898 ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno);
899
900 android_dlextinfo extinfo;
901 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
902 extinfo.library_fd = memfd;
903 extinfo.library_namespace = ns;
904
905 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
906
907 ASSERT_DL_NOTNULL(handle);
908
909 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
910 ASSERT_DL_NOTNULL(taxicab_number);
911 EXPECT_EQ(1729U, *taxicab_number);
912 dlclose(handle);
913}
914
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800915TEST(dlext, ns_symbol_visibilty_one_namespace) {
916 static const char* root_lib = "libnstest_root.so";
917 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
918
919 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs:" +
920 get_testlib_root() + "/private_namespace_libs";
921
922 android_namespace_t* ns =
923 android_create_namespace("one",
924 nullptr,
925 ns_search_path.c_str(),
926 ANDROID_NAMESPACE_TYPE_ISOLATED,
927 nullptr,
928 nullptr);
929
930 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
931
932 android_dlextinfo extinfo;
933 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
934 extinfo.library_namespace = ns;
935
936 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
937 ASSERT_TRUE(handle != nullptr) << dlerror();
938
939 typedef const char* (*fn_t)();
940
941 // Check that relocation worked correctly
942 fn_t ns_get_internal_extern_string =
943 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
944 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
945 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
946
947 fn_t internal_extern_string_fn =
948 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
949 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
950 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
951}
952
953TEST(dlext, ns_symbol_visibilty_between_namespaces) {
954 static const char* root_lib = "libnstest_root.so";
955 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
956
957 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
958 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
959
960 android_namespace_t* ns_public =
961 android_create_namespace("public",
962 nullptr,
963 public_ns_search_path.c_str(),
964 ANDROID_NAMESPACE_TYPE_ISOLATED,
965 nullptr,
966 nullptr);
967
968 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
969
970 android_namespace_t* ns_private =
971 android_create_namespace("private",
972 nullptr,
973 private_ns_search_path.c_str(),
974 ANDROID_NAMESPACE_TYPE_ISOLATED,
975 nullptr,
976 nullptr);
977
978 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
979 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
980
981 android_dlextinfo extinfo;
982 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
983 extinfo.library_namespace = ns_private;
984
985 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
986 ASSERT_TRUE(handle != nullptr) << dlerror();
987
988 typedef const char* (*fn_t)();
989
990 // Check that relocation worked correctly
991 fn_t ns_get_internal_extern_string =
992 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
993 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
994 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
995 "ns_get_internal_extern_string() expected to return null but returns \"" <<
996 ns_get_internal_extern_string() << "\"";
997
998 fn_t internal_extern_string_fn =
999 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
1000 ASSERT_TRUE(internal_extern_string_fn == nullptr);
1001 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
1002}
1003
1004TEST(dlext, ns_unload_between_namespaces) {
1005 static const char* root_lib = "libnstest_root.so";
1006 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1007
1008 const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs";
1009 const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs";
1010
1011 android_namespace_t* ns_public =
1012 android_create_namespace("public",
1013 nullptr,
1014 public_ns_search_path.c_str(),
1015 ANDROID_NAMESPACE_TYPE_ISOLATED,
1016 nullptr,
1017 nullptr);
1018
1019 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1020
1021 android_namespace_t* ns_private =
1022 android_create_namespace("private",
1023 nullptr,
1024 private_ns_search_path.c_str(),
1025 ANDROID_NAMESPACE_TYPE_ISOLATED,
1026 nullptr,
1027 nullptr);
1028
1029 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
1030 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1031
1032 android_dlextinfo extinfo;
1033 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1034 extinfo.library_namespace = ns_private;
1035
1036 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1037 ASSERT_TRUE(handle != nullptr) << dlerror();
1038
1039 dlclose(handle);
1040 // Check that root_lib was unloaded
1041 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1042 ASSERT_TRUE(handle == nullptr);
1043 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
1044 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1045
1046 // Check that shared library was unloaded in public ns
1047 extinfo.library_namespace = ns_public;
1048 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1049 ASSERT_TRUE(handle == nullptr);
1050 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
1051 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1052}
1053
Jiyong Park37b91af2017-05-05 22:07:05 +09001054TEST(dlext, ns_greylist_enabled) {
Dimitry Ivanov18623142017-02-21 13:41:08 -08001055 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1056
1057 const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
1058
1059 android_namespace_t* ns =
1060 android_create_namespace("namespace",
1061 nullptr,
1062 ns_search_path.c_str(),
Jiyong Park37b91af2017-05-05 22:07:05 +09001063 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED,
Dimitry Ivanov18623142017-02-21 13:41:08 -08001064 nullptr,
1065 nullptr);
1066
1067 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1068
1069 android_dlextinfo extinfo;
1070 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1071 extinfo.library_namespace = ns;
1072
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001073 // An app targeting M can open libnativehelper.so because it's on the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001074 android_set_application_target_sdk_version(__ANDROID_API_M__);
1075 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1076 ASSERT_TRUE(handle != nullptr) << dlerror();
1077
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001078 // Check that loader did not load another copy of libdl.so while loading greylisted library.
1079 void* dlsym_ptr = dlsym(handle, "dlsym");
1080 ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror();
1081 ASSERT_EQ(&dlsym, dlsym_ptr);
1082
Dimitry Ivanov18623142017-02-21 13:41:08 -08001083 dlclose(handle);
1084
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001085 // An app targeting N no longer has the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001086 android_set_application_target_sdk_version(__ANDROID_API_N__);
1087 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1088 ASSERT_TRUE(handle == nullptr);
1089 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1090}
1091
Jiyong Park37b91af2017-05-05 22:07:05 +09001092TEST(dlext, ns_greylist_disabled_by_default) {
1093 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1094
1095 const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
1096
1097 android_namespace_t* ns =
1098 android_create_namespace("namespace",
1099 nullptr,
1100 ns_search_path.c_str(),
1101 ANDROID_NAMESPACE_TYPE_ISOLATED,
1102 nullptr,
1103 nullptr);
1104
1105 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1106
1107 android_dlextinfo extinfo;
1108 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1109 extinfo.library_namespace = ns;
1110
1111 android_set_application_target_sdk_version(__ANDROID_API_M__);
1112 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1113 ASSERT_TRUE(handle == nullptr);
1114 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1115}
1116
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001117TEST(dlext, ns_cyclic_namespaces) {
1118 // Test that ns1->ns2->ns1 link does not break the loader
1119 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1120 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
1121
1122 const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs";
1123
1124 android_namespace_t* ns1 =
1125 android_create_namespace("ns1",
1126 nullptr,
1127 ns_search_path.c_str(),
1128 ANDROID_NAMESPACE_TYPE_ISOLATED,
1129 nullptr,
1130 nullptr);
1131
1132 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
1133
1134 android_namespace_t* ns2 =
1135 android_create_namespace("ns1",
1136 nullptr,
1137 ns_search_path.c_str(),
1138 ANDROID_NAMESPACE_TYPE_ISOLATED,
1139 nullptr,
1140 nullptr);
1141
1142 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
1143
1144 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
1145 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
1146
1147 android_dlextinfo extinfo;
1148 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1149 extinfo.library_namespace = ns1;
1150
1151 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
1152 ASSERT_TRUE(handle == nullptr);
1153 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
1154}
1155
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001156TEST(dlext, ns_isolated) {
1157 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001158 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001159
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001160 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001161 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001162 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1163
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08001164 android_set_application_target_sdk_version(42U); // something > 23
1165
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001166 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001167
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001168 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001169 android_create_namespace("private",
1170 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001171 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001172 ANDROID_NAMESPACE_TYPE_REGULAR,
1173 nullptr,
1174 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001175 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001176 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001177
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001178 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001179 android_create_namespace("private_isolated1",
1180 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001181 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001182 ANDROID_NAMESPACE_TYPE_ISOLATED,
1183 nullptr,
1184 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001185 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001186 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001187
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001188 android_namespace_t* ns_isolated2 =
1189 android_create_namespace("private_isolated2",
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001190 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001191 nullptr,
1192 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001193 get_testlib_root().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001194 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001195 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001196 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001197
1198 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1199 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1200
1201 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001202 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001203
1204 // Load lib_private_external_path to default namespace
1205 // (it should remain invisible for the isolated namespaces after this)
1206 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1207 ASSERT_TRUE(handle != nullptr) << dlerror();
1208
1209 android_dlextinfo extinfo;
1210 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1211 extinfo.library_namespace = ns_not_isolated;
1212
1213 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1214 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1215
1216 extinfo.library_namespace = ns_isolated;
1217
1218 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1219 ASSERT_TRUE(handle2 == nullptr);
1220 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1221
1222 // Check dlopen by absolute path
1223 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1224 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001225 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001226 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001227 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001228
1229 extinfo.library_namespace = ns_isolated2;
1230
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001231 // this should work because isolation_path for private_isolated2 includes get_testlib_root()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001232 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001233 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1234 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001235
1236 // Check dlopen by absolute path
1237 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001238 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1239 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001240
1241 typedef const char* (*fn_t)();
1242 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1243 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1244
1245 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1246
1247 fn_t ns_get_private_extern_string =
1248 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1249 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1250
1251 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1252
1253 fn_t ns_get_public_extern_string =
1254 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1255 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1256
1257 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1258
1259 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1260 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1261
1262 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1263
1264 dlclose(handle1);
1265}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001266
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001267TEST(dlext, ns_shared) {
1268 static const char* root_lib = "libnstest_root_not_isolated.so";
1269 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001270
1271 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001272
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001273 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001274 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1275 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1276
1277 android_set_application_target_sdk_version(42U); // something > 23
1278
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001279 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001280
1281 // preload this library to the default namespace to check if it
1282 // is shared later on.
1283 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001284 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001285 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1286
1287 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001288 android_create_namespace("private",
1289 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001290 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001291 ANDROID_NAMESPACE_TYPE_REGULAR,
1292 nullptr,
1293 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001294 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001295 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001296
1297 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001298 android_create_namespace("private_isolated_shared",
1299 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001300 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001301 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001302 nullptr,
1303 nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001304 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001305 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001306
1307 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1308 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1309
1310 std::string lib_private_external_path =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001311 get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001312
1313 // Load lib_private_external_path to default namespace
1314 // (it should remain invisible for the isolated namespaces after this)
1315 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1316 ASSERT_TRUE(handle != nullptr) << dlerror();
1317
1318 android_dlextinfo extinfo;
1319 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1320 extinfo.library_namespace = ns_not_isolated;
1321
1322 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1323 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1324
1325 extinfo.library_namespace = ns_isolated_shared;
1326
1327 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1328 ASSERT_TRUE(handle2 == nullptr);
1329 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1330
1331 // Check dlopen by absolute path
1332 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1333 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001334 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07001335 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001336 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001337
1338 // load libnstest_root.so to shared namespace in order to check that everything is different
1339 // except shared libnstest_dlopened.so
1340
1341 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1342
1343 typedef const char* (*fn_t)();
1344 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1345 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1346 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1347 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1348
1349 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1350 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1351 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1352
1353 fn_t ns_get_private_extern_string =
1354 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1355 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1356 fn_t ns_get_private_extern_string_shared =
1357 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1358 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1359
1360 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1361 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1362 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1363
1364 fn_t ns_get_public_extern_string =
1365 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1366 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1367 fn_t ns_get_public_extern_string_shared =
1368 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1369 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1370
1371 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1372 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1373 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1374
1375 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1376 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1377 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1378 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1379 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1380 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1381
1382 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1383 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1384 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1385 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1386 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1387
1388 dlclose(handle1);
1389 dlclose(handle2);
1390}
1391
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001392TEST(dlext, ns_shared_links_and_paths) {
1393 // Create parent namespace (isolated, not shared)
1394 android_namespace_t* ns_isolated =
1395 android_create_namespace("private_isolated",
1396 nullptr,
1397 (get_testlib_root() + "/private_namespace_libs").c_str(),
1398 ANDROID_NAMESPACE_TYPE_ISOLATED,
1399 (get_testlib_root() + "/public_namespace_libs").c_str(),
1400 nullptr);
1401 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
1402 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
1403
1404 // Create shared namespace with ns_isolated parent
1405 android_namespace_t* ns_shared =
1406 android_create_namespace("private_shared",
1407 nullptr,
1408 nullptr,
1409 ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED,
1410 nullptr,
1411 ns_isolated);
1412 ASSERT_TRUE(ns_shared != nullptr) << dlerror();
1413
1414 // 1. Load a library in ns_shared to check that it has inherited
1415 // search path and the link to the default namespace.
1416 android_dlextinfo extinfo;
1417 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1418 extinfo.library_namespace = ns_shared;
1419
1420 {
1421 void* handle = android_dlopen_ext("libnstest_private.so", RTLD_NOW, &extinfo);
1422 ASSERT_TRUE(handle != nullptr) << dlerror();
1423 const char** ns_private_extern_string = static_cast<const char**>(dlsym(handle, "g_private_extern_string"));
1424 ASSERT_TRUE(ns_private_extern_string != nullptr) << dlerror();
1425 ASSERT_STREQ("This string is from private namespace", *ns_private_extern_string);
1426
1427 dlclose(handle);
1428 }
1429 // 2. Load another test library by absolute path to check that
1430 // it has inherited permitted_when_isolated_path
1431 {
1432 void* handle = android_dlopen_ext(
1433 (get_testlib_root() + "/public_namespace_libs/libnstest_public.so").c_str(),
1434 RTLD_NOW,
1435 &extinfo);
1436
1437 ASSERT_TRUE(handle != nullptr) << dlerror();
1438 const char** ns_public_extern_string = static_cast<const char**>(dlsym(handle, "g_public_extern_string"));
1439 ASSERT_TRUE(ns_public_extern_string != nullptr) << dlerror();
1440 ASSERT_STREQ("This string is from public namespace", *ns_public_extern_string);
1441
1442 dlclose(handle);
1443 }
1444
1445 // 3. Check that it is still isolated.
1446 {
1447 void* handle = android_dlopen_ext(
1448 (get_testlib_root() + "/libtest_empty.so").c_str(),
1449 RTLD_NOW,
1450 &extinfo);
1451
1452 ASSERT_TRUE(handle == nullptr);
1453 }
1454}
1455
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001456TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001457 android_set_application_target_sdk_version(42U); // something > 23
1458
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001459 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001460
1461 // preload this library to the default namespace to check if it
1462 // is shared later on.
1463 void* handle_dlopened =
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001464 dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001465 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1466
1467 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001468 android_create_namespace("private_isolated_shared",
1469 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001470 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001471 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001472 nullptr,
1473 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001474 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001475 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001476
1477 // Check if "libnstest_dlopened.so" is loaded (and the same)
1478 android_dlextinfo extinfo;
1479 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1480 extinfo.library_namespace = ns_isolated_shared;
1481
1482 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1483 ASSERT_TRUE(handle != nullptr) << dlerror();
1484 ASSERT_TRUE(handle == handle_dlopened);
1485 dlclose(handle);
1486 dlclose(handle_dlopened);
1487
1488 // And now check that the library cannot be found by soname (and is no longer loaded)
1489 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1490 ASSERT_TRUE(handle == nullptr)
1491 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1492
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001493 handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001494 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1495 ASSERT_TRUE(handle == nullptr)
1496 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1497
1498 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1499 ASSERT_TRUE(handle == nullptr)
1500 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1501
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001502 handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001503 RTLD_NOW | RTLD_NOLOAD);
1504 ASSERT_TRUE(handle == nullptr)
1505 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1506
1507 // Now lets see if the soinfo area gets reused in the wrong way:
1508 // load a library to default namespace.
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001509 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001510 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1511 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1512
1513 // try to find it in shared namespace
1514 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1515 ASSERT_TRUE(handle == nullptr)
1516 << "Error: " << g_public_lib << " is accessible in shared namespace";
1517}
1518
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001519TEST(dlext, ns_isolated_rtld_global) {
1520 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001521 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001522
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001523 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001524
1525 android_namespace_t* ns1 =
1526 android_create_namespace("isolated1",
1527 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001528 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001529 ANDROID_NAMESPACE_TYPE_ISOLATED,
1530 lib_public_path.c_str(),
1531 nullptr);
1532 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001533 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001534
1535 android_namespace_t* ns2 =
1536 android_create_namespace("isolated2",
1537 nullptr,
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001538 (get_testlib_root() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001539 ANDROID_NAMESPACE_TYPE_ISOLATED,
1540 lib_public_path.c_str(),
1541 nullptr);
1542 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001543 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001544
1545 android_dlextinfo extinfo;
1546 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1547 extinfo.library_namespace = ns1;
1548
1549 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1550 RTLD_GLOBAL,
1551 &extinfo);
1552
1553 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1554
1555 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001556 android_create_namespace("isolated1_child",
1557 nullptr,
1558 (get_testlib_root() + "/private_namespace_libs").c_str(),
1559 ANDROID_NAMESPACE_TYPE_ISOLATED,
1560 nullptr,
1561 ns1);
1562
1563 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1564 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001565
1566 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1567 // attempt to use ns2 should result in dlerror()
1568
1569 // Check ns1_child first.
1570 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1571 extinfo.library_namespace = ns1_child;
1572
1573 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1574 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1575
1576 // now ns1
1577 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1578 extinfo.library_namespace = ns1;
1579
1580 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1581 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1582
1583 // and ns2 should fail
1584 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1585 extinfo.library_namespace = ns2;
1586
1587 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1588 ASSERT_TRUE(handle1 == nullptr);
1589 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1590}
1591
dimitry8db36a52017-10-23 15:10:10 +02001592TEST(dlext, ns_inaccessible_error_message) {
1593 // We set up 2 namespaces (a and b) and link a->b with a shared library
1594 // libtestshared.so. Then try to dlopen different library with the same
1595 // name from in namespace a. Note that library should not be accessible
1596 // in either namespace but since it's soname is in the list of shared libs
1597 // the linker will attempt to find it in linked namespace.
1598 //
1599 // Check the error message and make sure it mentions correct namespace name.
1600 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1601
1602 android_namespace_t* ns_a =
1603 android_create_namespace("ns_a",
1604 nullptr,
1605 (get_testlib_root() + "/private_namespace_libs").c_str(),
1606 ANDROID_NAMESPACE_TYPE_ISOLATED,
1607 nullptr,
1608 nullptr);
1609 ASSERT_TRUE(ns_a != nullptr) << dlerror();
1610 ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
1611
1612 android_namespace_t* ns_b =
1613 android_create_namespace("ns_b",
1614 nullptr,
1615 get_testlib_root().c_str(),
1616 ANDROID_NAMESPACE_TYPE_ISOLATED,
1617 nullptr,
1618 nullptr);
1619 ASSERT_TRUE(ns_b != nullptr) << dlerror();
1620 ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
1621
1622 ASSERT_TRUE(android_link_namespaces(ns_a, ns_b, "libtestshared.so")) << dlerror();
1623
1624 android_dlextinfo extinfo;
1625 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1626 extinfo.library_namespace = ns_a;
1627
1628 std::string library_path = get_testlib_root() + "/inaccessible_libs/libtestshared.so";
1629
1630 void* handle = android_dlopen_ext(library_path.c_str(), RTLD_NOW, &extinfo);
1631 ASSERT_TRUE(handle == nullptr);
1632 std::string expected_dlerror =
1633 android::base::StringPrintf("dlopen failed: library \"%s\" needed or dlopened by \"%s\""
1634 " is not accessible for the namespace \"ns_a\"",
1635 library_path.c_str(),
1636 get_executable_path().c_str());
1637 ASSERT_EQ(expected_dlerror, dlerror());
1638}
1639
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001640TEST(dlext, ns_anonymous) {
1641 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001642 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001643
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001644 const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001645 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1646
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001647 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1648
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001649 ASSERT_TRUE(
1650 android_init_anonymous_namespace(shared_libs.c_str(),
1651 (get_testlib_root() + "/private_namespace_libs").c_str())
1652 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001653
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001654 android_namespace_t* ns =
1655 android_create_namespace("private",
1656 nullptr,
1657 (get_testlib_root() + "/private_namespace_libs").c_str(),
1658 ANDROID_NAMESPACE_TYPE_REGULAR,
1659 nullptr,
1660 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001661
1662 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001663 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001664
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -08001665 std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001666
1667 android_dlextinfo extinfo;
1668 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1669 extinfo.library_namespace = ns;
1670
1671 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1672 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1673 ASSERT_TRUE(handle != nullptr) << dlerror();
1674
1675 uintptr_t ns_get_dlopened_string_addr =
1676 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1677 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1678 typedef const char* (*fn_t)();
1679 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1680
1681 std::vector<map_record> maps;
1682 Maps::parse_maps(&maps);
1683
1684 uintptr_t addr_start = 0;
1685 uintptr_t addr_end = 0;
dimitry8eaf28d2017-10-11 10:04:14 +02001686 bool has_executable_segment = false;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001687 std::vector<map_record> maps_to_copy;
1688
1689 for (const auto& rec : maps) {
1690 if (rec.pathname == private_library_absolute_path) {
1691 if (addr_start == 0) {
1692 addr_start = rec.addr_start;
1693 }
1694 addr_end = rec.addr_end;
dimitry8eaf28d2017-10-11 10:04:14 +02001695 has_executable_segment = has_executable_segment || (rec.perms & PROT_EXEC) != 0;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001696
1697 maps_to_copy.push_back(rec);
1698 }
1699 }
1700
1701 // some sanity checks..
1702 ASSERT_TRUE(addr_start > 0);
1703 ASSERT_TRUE(addr_end > 0);
1704 ASSERT_EQ(3U, maps_to_copy.size());
1705 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1706 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1707
dimitry8eaf28d2017-10-11 10:04:14 +02001708 if (!has_executable_segment) {
1709 // For some natively bridged environments this code might be missing
1710 // the executable flag. This is because the guest code is not supposed
1711 // to be executed directly and making it non-executable is more secure.
1712 // If this is the case we assume that the first segment is the one that
1713 // has this flag.
1714 ASSERT_TRUE((maps_to_copy[0].perms & PROT_WRITE) == 0);
1715 maps_to_copy[0].perms |= PROT_EXEC;
1716 }
1717
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001718 // copy
1719 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1720 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1721 -1, 0));
1722 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1723
1724 for (const auto& rec : maps_to_copy) {
1725 uintptr_t offset = rec.addr_start - addr_start;
1726 size_t size = rec.addr_end - rec.addr_start;
1727 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1728 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1729 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1730 ASSERT_TRUE(map != MAP_FAILED);
1731 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1732 mprotect(map, size, rec.perms);
1733 }
1734
1735 // call the function copy
1736 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1737 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1738 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1739 ns_get_dlopened_string_anon());
1740
1741 // They should belong to different namespaces (private and anonymous)
1742 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1743 ns_get_dlopened_string_private());
1744
1745 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1746}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001747
1748TEST(dlext, dlopen_handle_value_platform) {
1749 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1750 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1751 << "dlopen should return odd value for the handle";
1752 dlclose(handle);
1753}
1754
1755TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001756 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001757 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1758 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1759 << "dlopen should return valid pointer";
1760 dlclose(handle);
1761}