blob: e98d66ffd3f078edde90eba26b86ccef0e05b487 [file] [log] [blame]
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080020#include <elf.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000021#include <errno.h>
22#include <fcntl.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080023#include <inttypes.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000024#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070027
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000028#include <android/dlext.h>
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -070029#include <android-base/file.h>
Zhenhua WANG81aad002017-04-25 11:07:19 +080030#include <android-base/strings.h>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070031
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
Sandeep Patil4e02cc12019-01-21 14:22:05 -080037#include <meminfo/procmeminfo.h>
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -040038#include <procinfo/process_map.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"
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";
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -040061constexpr const char* kLibNameRecursive = "libdlext_test_recursive.so";
Dimitry Ivanov927877c2016-09-21 11:17:13 -070062constexpr 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:
Yi Kong358603a2019-03-29 14:25:16 -070068 void SetUp() override {
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
Yi Kong358603a2019-03-29 14:25:16 -070079 void TearDown() override {
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) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700107 const std::string lib_path = GetTestlibRoot() + "/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) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700125 const std::string lib_path = GetTestlibRoot() + "/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;
Elliott Hughesb51bb502019-05-03 22:45:41 -0700135 ASSERT_EQ(0, FindEntry(handle, kLibZipSimpleZip, &zip_entry));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800136 extinfo.library_fd_offset = zip_entry.offset;
137 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700138
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700139 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700140 ASSERT_DL_NOTNULL(handle_);
141
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700142 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
143 ASSERT_DL_NOTNULL(taxicab_number);
144 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700145}
146
147TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700148 const std::string lib_path = GetTestlibRoot() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700149
150 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700151 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700152 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700153 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700154
155 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
156 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700157 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
158
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800159 // Test an address above 2^44, for http://b/18178121 .
160 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700161 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700162 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800163 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
164
165 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
166 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
167 ASSERT_TRUE(handle_ == nullptr);
168 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
169
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700170 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700171 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800172 ASSERT_TRUE(handle_ == nullptr);
Elliott Hughesa8971512018-06-27 14:39:06 -0700173 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic: 504b0304", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700174
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800175 // Check if dlsym works after unsuccessful dlopen().
176 // Supply non-exiting one to make linker visit every soinfo.
177 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
178 ASSERT_TRUE(sym == nullptr);
179
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700180 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700181}
182
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800183TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700184 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700185 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800186 // This offset will not be used, so it doesn't matter.
187 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700188
189 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
190 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700191 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 -0700192}
193
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700194TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700195 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
196 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700197 // 1. Open actual file
198 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
199 ASSERT_DL_NOTNULL(handle);
200 // 2. Open link with force_load flag set
201 android_dlextinfo extinfo;
202 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700203 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700204 ASSERT_DL_NOTNULL(handle2);
205 ASSERT_TRUE(handle != handle2);
206
207 dlclose(handle2);
208 dlclose(handle);
209}
210
211TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700212 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
213 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700214 // Check if soname lookup still returns already loaded library
215 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700216 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700217 ASSERT_DL_NOTNULL(handle);
218
219 android_dlextinfo extinfo;
220 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
221
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700222 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700223 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
224
225 ASSERT_DL_NOTNULL(handle2);
226 ASSERT_TRUE(handle == handle2);
227
228 dlclose(handle2);
229 dlclose(handle);
230}
231
Victor Chang6cb719f2019-02-06 17:19:10 +0000232TEST(dlfcn, dlopen_from_nullptr_android_api_level) {
233 // Regression test for http://b/123972211. Testing dlopen(nullptr) when target sdk is P
234 android_set_application_target_sdk_version(__ANDROID_API_P__);
235 ASSERT_TRUE(dlopen(nullptr, RTLD_NOW) != nullptr);
236}
237
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700238TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700239 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700240 const std::string lib_path = GetTestlibRoot() + lib_zip_path;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700241
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700242 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700243 ASSERT_TRUE(handle != nullptr) << dlerror();
244
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700245 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
246 ASSERT_DL_NOTNULL(taxicab_number);
247 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700248
249 dlclose(handle);
250}
251
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700252TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700253 const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700254 const std::string lib_path = GetTestlibRoot() + lib_zip_path;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700255
256 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
257
258 ASSERT_TRUE(handle != nullptr) << dlerror();
259
260 typedef void *(* dlopen_b_fn)();
261 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
262 ASSERT_TRUE(fn != nullptr) << dlerror();
263
264 void *p = fn();
265 ASSERT_TRUE(p != nullptr) << dlerror();
266
267 dlclose(p);
268 dlclose(handle);
269}
270
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700271TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700272 const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700273 const std::string lib_path = GetTestlibRoot() + lib_zip_path + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700274
275 typedef void (*fn_t)(const char*);
276 fn_t android_update_LD_LIBRARY_PATH =
277 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
278
279 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
280
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700281 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700282 ASSERT_TRUE(handle == nullptr);
283
284 android_update_LD_LIBRARY_PATH(lib_path.c_str());
285
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700286 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700287 ASSERT_TRUE(handle != nullptr) << dlerror();
288
289 int (*fn)(void);
290 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
291 ASSERT_TRUE(fn != nullptr);
292 EXPECT_EQ(4, fn());
293
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800294 uint32_t* taxicab_number =
295 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700296 ASSERT_DL_NOTNULL(taxicab_number);
297 EXPECT_EQ(1729U, *taxicab_number);
298
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700299 dlclose(handle);
300}
301
302
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000303TEST_F(DlExtTest, Reserved) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700304 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000305 ASSERT_TRUE(start != MAP_FAILED);
306 android_dlextinfo extinfo;
307 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
308 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700309 extinfo.reserved_size = kLibSize;
310 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000311 ASSERT_DL_NOTNULL(handle_);
312 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
313 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700314 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000315 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700316 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000317 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800318
319 // Check that after dlclose reserved address space is unmapped (and can be reused)
320 dlclose(handle_);
321 handle_ = nullptr;
322
323 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
324 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000325}
326
327TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800328 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000329 ASSERT_TRUE(start != MAP_FAILED);
330 android_dlextinfo extinfo;
331 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
332 extinfo.reserved_addr = start;
333 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700334 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700335 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000336}
337
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400338TEST_F(DlExtTest, ReservedRecursive) {
339 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
340 ASSERT_TRUE(start != MAP_FAILED);
341 android_dlextinfo extinfo;
342 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
343 extinfo.reserved_addr = start;
344 extinfo.reserved_size = kLibSize;
345 handle_ = android_dlopen_ext(kLibNameRecursive, RTLD_NOW, &extinfo);
346 ASSERT_DL_NOTNULL(handle_);
347
348 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
349 ASSERT_DL_NOTNULL(f);
350 EXPECT_GE(reinterpret_cast<void*>(f), start);
351 EXPECT_LT(reinterpret_cast<void*>(f),
352 reinterpret_cast<char*>(start) + kLibSize);
353 EXPECT_EQ(4, f());
354
355 f = reinterpret_cast<fn>(dlsym(handle_, "getBiggerRandomNumber"));
356 ASSERT_DL_NOTNULL(f);
357 EXPECT_GE(reinterpret_cast<void*>(f), start);
358 EXPECT_LT(reinterpret_cast<void*>(f),
359 reinterpret_cast<char*>(start) + kLibSize);
360 EXPECT_EQ(8, f());
361
362 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
363 ASSERT_DL_NOTNULL(taxicab_number);
Peter Collingbourne191ecdc2019-08-07 19:06:00 -0700364 // Untag the pointer so that it can be compared with start, which will be untagged.
365 void* addr = reinterpret_cast<void*>(untag_address(taxicab_number));
366 EXPECT_GE(addr, start);
367 EXPECT_LT(addr, reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400368 EXPECT_EQ(1729U, *taxicab_number);
369}
370
371TEST_F(DlExtTest, ReservedRecursiveTooSmall) {
Yi Kong1e7a1502019-03-14 16:25:45 -0700372 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400373 ASSERT_TRUE(start != MAP_FAILED);
374 android_dlextinfo extinfo;
375 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
376 extinfo.reserved_addr = start;
Yi Kong1e7a1502019-03-14 16:25:45 -0700377 extinfo.reserved_size = PAGE_SIZE;
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400378 handle_ = android_dlopen_ext(kLibNameRecursive, RTLD_NOW, &extinfo);
379 EXPECT_EQ(nullptr, handle_);
380}
381
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000382TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700383 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000384 ASSERT_TRUE(start != MAP_FAILED);
385 android_dlextinfo extinfo;
386 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
387 extinfo.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700388 extinfo.reserved_size = kLibSize;
389 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000390 ASSERT_DL_NOTNULL(handle_);
391 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
392 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700393 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000394 EXPECT_LT(reinterpret_cast<void*>(f),
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700395 reinterpret_cast<char*>(start) + kLibSize);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000396 EXPECT_EQ(4, f());
397}
398
399TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800400 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000401 ASSERT_TRUE(start != MAP_FAILED);
402 android_dlextinfo extinfo;
403 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
404 extinfo.reserved_addr = start;
405 extinfo.reserved_size = PAGE_SIZE;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700406 handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000407 ASSERT_DL_NOTNULL(handle_);
408 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
409 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700410 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
411 (reinterpret_cast<void*>(f) >=
412 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000413 EXPECT_EQ(4, f());
414}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000415
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100416class DlExtRelroSharingTest : public DlExtTest {
417protected:
Yi Kong358603a2019-03-29 14:25:16 -0700418 void SetUp() override {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100419 DlExtTest::SetUp();
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700420 void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100421 ASSERT_TRUE(start != MAP_FAILED);
422 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
423 extinfo_.reserved_addr = start;
Dimitry Ivanov927877c2016-09-21 11:17:13 -0700424 extinfo_.reserved_size = kLibSize;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100425 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000426 }
427
Yi Kong358603a2019-03-29 14:25:16 -0700428 void TearDown() override {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100429 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100430 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000431
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400432 void CreateRelroFile(const char* lib, const char* relro_file, bool recursive) {
Elliott Hughes5cec3772018-01-19 15:45:23 -0800433 int relro_fd = open(relro_file, O_RDWR | O_TRUNC | O_CLOEXEC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100434 ASSERT_NOERROR(relro_fd);
435
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400436 if (recursive) {
437 extinfo_.flags |= ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
438 }
439
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100440 pid_t pid = fork();
441 if (pid == 0) {
442 // child process
443 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
444 extinfo_.relro_fd = relro_fd;
445 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700446 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100447 fprintf(stderr, "in child: %s\n", dlerror());
448 exit(1);
449 }
Torne (Richard Coles)fa9f7f22019-04-02 17:04:42 -0400450 fn f = reinterpret_cast<fn>(dlsym(handle, "getRandomNumber"));
451 ASSERT_DL_NOTNULL(f);
452 EXPECT_EQ(4, f());
453
454 if (recursive) {
455 fn f = reinterpret_cast<fn>(dlsym(handle, "getBiggerRandomNumber"));
456 ASSERT_DL_NOTNULL(f);
457 EXPECT_EQ(8, f());
458 }
459
460 uint32_t* taxicab_number =
461 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
462 ASSERT_DL_NOTNULL(taxicab_number);
463 EXPECT_EQ(1729U, *taxicab_number);
464 exit(testing::Test::HasFailure());
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100465 }
466
467 // continuing in parent
468 ASSERT_NOERROR(close(relro_fd));
469 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800470 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100471
472 // reopen file for reading so it can be used
Elliott Hughes5cec3772018-01-19 15:45:23 -0800473 relro_fd = open(relro_file, O_RDONLY | O_CLOEXEC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100474 ASSERT_NOERROR(relro_fd);
475 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
476 extinfo_.relro_fd = relro_fd;
477 }
478
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400479 void TryUsingRelro(const char* lib, bool recursive) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100480 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
481 ASSERT_DL_NOTNULL(handle_);
482 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
483 ASSERT_DL_NOTNULL(f);
484 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700485
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400486 if (recursive) {
487 fn f = reinterpret_cast<fn>(dlsym(handle_, "getBiggerRandomNumber"));
488 ASSERT_DL_NOTNULL(f);
489 EXPECT_EQ(8, f());
490 }
491
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800492 uint32_t* taxicab_number =
493 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700494 ASSERT_DL_NOTNULL(taxicab_number);
495 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100496 }
497
Zhenhua WANG81aad002017-04-25 11:07:19 +0800498 void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro,
499 size_t* pss_out);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100500
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400501 std::string FindMappingName(void* ptr);
502
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100503 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100504};
505
506TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800507 TemporaryFile tf; // Use tf to get an unique filename.
508 ASSERT_NOERROR(close(tf.fd));
509
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400510 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path, false));
511 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName, false));
512 void* relro_data = dlsym(handle_, "lots_of_relro");
513 ASSERT_DL_NOTNULL(relro_data);
514 EXPECT_EQ(tf.path, FindMappingName(relro_data));
515
516 // Use destructor of tf to close and unlink the file.
517 tf.fd = extinfo_.relro_fd;
518}
519
520TEST_F(DlExtRelroSharingTest, ChildWritesGoodDataRecursive) {
521 TemporaryFile tf; // Use tf to get an unique filename.
522 ASSERT_NOERROR(close(tf.fd));
523
524 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameRecursive, tf.path, true));
525 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameRecursive, true));
526 void* relro_data = dlsym(handle_, "lots_of_relro");
527 ASSERT_DL_NOTNULL(relro_data);
528 EXPECT_EQ(tf.path, FindMappingName(relro_data));
529 void* recursive_relro_data = dlsym(handle_, "lots_more_relro");
530 ASSERT_DL_NOTNULL(recursive_relro_data);
531 EXPECT_EQ(tf.path, FindMappingName(recursive_relro_data));
532
Yabin Cui294d1e22014-12-07 20:43:37 -0800533
534 // Use destructor of tf to close and unlink the file.
535 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100536}
537
Torne (Richard Coles)5d103742019-04-11 12:25:06 -0400538TEST_F(DlExtRelroSharingTest, CheckRelroSizes) {
539 TemporaryFile tf1, tf2;
540 ASSERT_NOERROR(close(tf1.fd));
541 ASSERT_NOERROR(close(tf2.fd));
542
543 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameRecursive, tf1.path, false));
544 struct stat no_recursive;
545 ASSERT_NOERROR(fstat(extinfo_.relro_fd, &no_recursive));
546 tf1.fd = extinfo_.relro_fd;
547
548 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameRecursive, tf2.path, true));
549 struct stat with_recursive;
550 ASSERT_NOERROR(fstat(extinfo_.relro_fd, &with_recursive));
551 tf2.fd = extinfo_.relro_fd;
552
553 // RELRO file should end up bigger when we use the recursive flag, since it
554 // includes data for more than one library.
555 ASSERT_GT(with_recursive.st_size, no_recursive.st_size);
556}
557
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100558TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800559 TemporaryFile tf; // // Use tf to get an unique filename.
560 ASSERT_NOERROR(close(tf.fd));
561
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400562 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.path, false));
563 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro, false));
Yabin Cui294d1e22014-12-07 20:43:37 -0800564
565 // Use destructor of tf to close and unlink the file.
566 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100567}
568
569TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400570 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName, false));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000571}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100572
573TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800574 if (geteuid() != 0) GTEST_SKIP() << "This test must be run as root";
Dan Albert69fb9f32014-09-03 11:30:21 -0700575
Yabin Cui294d1e22014-12-07 20:43:37 -0800576 TemporaryFile tf; // Use tf to get an unique filename.
577 ASSERT_NOERROR(close(tf.fd));
578
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400579 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path, false));
Yabin Cui294d1e22014-12-07 20:43:37 -0800580
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100581 int pipefd[2];
582 ASSERT_NOERROR(pipe(pipefd));
583
584 size_t without_sharing, with_sharing;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800585 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.path, false, &without_sharing));
586 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.path, true, &with_sharing));
Zhenhua WANG81aad002017-04-25 11:07:19 +0800587 ASSERT_LT(with_sharing, without_sharing);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100588
Zhenhua WANG81aad002017-04-25 11:07:19 +0800589 // We expect the sharing to save at least 50% of the library's total PSS.
590 // In practice it saves 80%+ for this library in the test.
591 size_t pss_saved = without_sharing - with_sharing;
592 size_t expected_min_saved = without_sharing / 2;
593
594 EXPECT_LT(expected_min_saved, pss_saved);
Yabin Cui294d1e22014-12-07 20:43:37 -0800595
596 // Use destructor of tf to close and unlink the file.
597 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100598}
599
Zhenhua WANG81aad002017-04-25 11:07:19 +0800600void GetPss(bool shared_relro, const char* lib, const char* relro_file, pid_t pid,
601 size_t* total_pss) {
Sandeep Patil4e02cc12019-01-21 14:22:05 -0800602 android::meminfo::ProcMemInfo proc_mem(pid);
Christopher Ferris89b658c2019-10-10 13:27:54 -0700603 const std::vector<android::meminfo::Vma>& maps = proc_mem.MapsWithoutUsageStats();
Sandeep Patil4e02cc12019-01-21 14:22:05 -0800604 ASSERT_GT(maps.size(), 0UL);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100605
Zhenhua WANG81aad002017-04-25 11:07:19 +0800606 // Calculate total PSS of the library.
607 *total_pss = 0;
608 bool saw_relro_file = false;
Sandeep Patil4e02cc12019-01-21 14:22:05 -0800609 for (auto& vma : maps) {
610 if (android::base::EndsWith(vma.name, lib) || (vma.name == relro_file)) {
611 if (vma.name == relro_file) {
612 saw_relro_file = true;
613 }
Zhenhua WANG81aad002017-04-25 11:07:19 +0800614
Christopher Ferris89b658c2019-10-10 13:27:54 -0700615 android::meminfo::Vma update_vma(vma);
616 ASSERT_TRUE(proc_mem.FillInVmaStats(update_vma));
617 *total_pss += update_vma.usage.pss;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800618 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100619 }
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100620
Zhenhua WANG81aad002017-04-25 11:07:19 +0800621 if (shared_relro) ASSERT_TRUE(saw_relro_file);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100622}
623
Zhenhua WANG81aad002017-04-25 11:07:19 +0800624void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file,
625 bool share_relro, size_t* pss_out) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100626 const int CHILDREN = 20;
627
628 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800629 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100630 int childpipe[CHILDREN];
631 for (int i=0; i<CHILDREN; ++i) {
632 char read_buf;
633 int child_done_pipe[2], parent_done_pipe[2];
634 ASSERT_NOERROR(pipe(child_done_pipe));
635 ASSERT_NOERROR(pipe(parent_done_pipe));
636
637 pid_t child = fork();
638 if (child == 0) {
639 // close the 'wrong' ends of the pipes in the child
640 close(child_done_pipe[0]);
641 close(parent_done_pipe[1]);
642
643 // open the library
644 void* handle;
645 if (share_relro) {
646 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
647 } else {
648 handle = dlopen(lib, RTLD_NOW);
649 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700650 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100651 fprintf(stderr, "in child: %s\n", dlerror());
652 exit(1);
653 }
654
655 // close write end of child_done_pipe to signal the parent that we're done.
656 close(child_done_pipe[1]);
657
658 // wait for the parent to close parent_done_pipe, then exit
659 read(parent_done_pipe[0], &read_buf, 1);
660 exit(0);
661 }
662
663 ASSERT_NOERROR(child);
664
665 // close the 'wrong' ends of the pipes in the parent
666 close(child_done_pipe[1]);
667 close(parent_done_pipe[0]);
668
669 // wait for the child to be done
670 read(child_done_pipe[0], &read_buf, 1);
671 close(child_done_pipe[0]);
672
673 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800674 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100675 childpipe[i] = parent_done_pipe[1];
676 }
677
Zhenhua WANG81aad002017-04-25 11:07:19 +0800678 // Sum the PSS of tested library of all the children
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100679 size_t total_pss = 0;
680 for (int i=0; i<CHILDREN; ++i) {
681 size_t child_pss;
Zhenhua WANG81aad002017-04-25 11:07:19 +0800682 ASSERT_NO_FATAL_FAILURE(GetPss(share_relro, lib, relro_file, child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100683 total_pss += child_pss;
684 }
685 *pss_out = total_pss;
686
687 // Close pipes and wait for children to exit
688 for (int i=0; i<CHILDREN; ++i) {
689 ASSERT_NOERROR(close(childpipe[i]));
690 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800691 for (int i = 0; i < CHILDREN; ++i) {
692 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100693 }
694}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700695
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400696std::string DlExtRelroSharingTest::FindMappingName(void* ptr) {
697 uint64_t addr = reinterpret_cast<uint64_t>(ptr);
698 std::string found_name = "<not found>";
699
700 EXPECT_TRUE(android::procinfo::ReadMapFile(
701 "/proc/self/maps",
702 [&](uint64_t start, uint64_t end, uint16_t, uint16_t, ino_t, const char* name) {
703 if (addr >= start && addr < end) {
704 found_name = name;
705 }
706 }));
707
708 return found_name;
709}
710
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700711// Testing namespaces
712static const char* g_public_lib = "libnstest_public.so";
713
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800714// These are libs shared with default namespace
715static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so";
716
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700717TEST(dlext, ns_smoke) {
718 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800719 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700720
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800721 ASSERT_FALSE(android_init_anonymous_namespace("", nullptr));
722 ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces"
723 " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.",
724 dlerror());
Dimitry Ivanov54807612016-04-21 14:57:38 -0700725
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700726 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800727 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700728 ASSERT_TRUE(handle_public != nullptr) << dlerror();
729
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800730 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700731
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800732 // Check that libraries added to public namespace are not NODELETE
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700733 dlclose(handle_public);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800734 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800735 ASSERT_TRUE(handle_public == nullptr);
736 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
737 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
738
739 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700740
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800741 // create "public namespace", share limited set of public libraries with
742
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800743 android_namespace_t* ns1 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800744 android_create_namespace("private",
745 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700746 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800747 ANDROID_NAMESPACE_TYPE_REGULAR,
748 nullptr,
749 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700750 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800751 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700752
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800753 android_namespace_t* ns2 =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800754 android_create_namespace("private_isolated",
755 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700756 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800757 ANDROID_NAMESPACE_TYPE_ISOLATED,
758 nullptr,
759 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700760 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800761 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700762
763 // This should not have affect search path for default namespace:
764 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
765 void* handle = dlopen(g_public_lib, RTLD_NOW);
766 ASSERT_TRUE(handle != nullptr) << dlerror();
767 dlclose(handle);
768
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700769 // dlopen for a public library using an absolute path should work
770 // 1. For isolated namespaces
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700771 android_dlextinfo extinfo;
772 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -0700773 extinfo.library_namespace = ns2;
774 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
775 ASSERT_TRUE(handle != nullptr) << dlerror();
776 ASSERT_TRUE(handle == handle_public);
777
778 dlclose(handle);
779
780 // 1.1 even if it wasn't loaded before
781 dlclose(handle_public);
782
783 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
784 ASSERT_TRUE(handle_public == nullptr);
785 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
786 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
787
788 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
789 ASSERT_TRUE(handle != nullptr) << dlerror();
790
791 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
792 ASSERT_TRUE(handle == handle_public);
793
794 dlclose(handle);
795
796 // 2. And for regular namespaces (make sure it does not load second copy of the library)
797 extinfo.library_namespace = ns1;
798 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
799 ASSERT_TRUE(handle != nullptr) << dlerror();
800 ASSERT_TRUE(handle == handle_public);
801
802 dlclose(handle);
803
804 // 2.1 Unless it was not loaded before - in which case it will load a duplicate.
805 // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces?
806 dlclose(handle_public);
807
808 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD);
809 ASSERT_TRUE(handle_public == nullptr);
810 ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path +
811 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
812
813 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
814 ASSERT_TRUE(handle != nullptr) << dlerror();
815
816 handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
817
818 ASSERT_TRUE(handle != handle_public);
819
820 dlclose(handle);
821
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700822 extinfo.library_namespace = ns1;
823
824 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
825 ASSERT_TRUE(handle1 != nullptr) << dlerror();
826
827 extinfo.library_namespace = ns2;
828 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
829 ASSERT_TRUE(handle2 != nullptr) << dlerror();
830
831 ASSERT_TRUE(handle1 != handle2);
832
833 typedef const char* (*fn_t)();
834
835 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
836 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
837 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
838 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
839
840 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
841 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
842
843 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
844
845 fn_t ns_get_private_extern_string1 =
846 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
847 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
848 fn_t ns_get_private_extern_string2 =
849 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
850 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
851
852 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
853 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
854
855 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
856
857 fn_t ns_get_public_extern_string1 =
858 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
859 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
860 fn_t ns_get_public_extern_string2 =
861 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
862 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
863
864 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
865 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
866
867 // and now check that dlopen() does the right thing in terms of preserving namespace
868 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
869 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
870 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
871 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
872
873 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
874 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
875
876 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
877
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800878 // Check that symbols from non-shared libraries a shared library depends on are not visible
879 // from original namespace.
880
881 fn_t ns_get_internal_extern_string =
882 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string"));
883 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
884 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
885 "ns_get_internal_extern_string() expected to return null but returns \"" <<
886 ns_get_internal_extern_string() << "\"";
887
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700888 dlclose(handle1);
889
890 // Check if handle2 is still alive (and well)
891 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
892 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
893 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
894 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
895
896 dlclose(handle2);
897}
898
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700899TEST(dlext, dlopen_ext_use_o_tmpfile_fd) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700900 const std::string lib_path = GetTestlibRoot() + "/libtest_simple.so";
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700901
902 int tmpfd = TEMP_FAILURE_RETRY(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700903 open(GetTestlibRoot().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL, 0));
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700904
905 // Ignore kernels without O_TMPFILE flag support
906 if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) {
907 return;
908 }
909
910 ASSERT_TRUE(tmpfd != -1) << strerror(errno);
911
912 android_namespace_t* ns =
913 android_create_namespace("testing-o_tmpfile",
914 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700915 GetTestlibRoot().c_str(),
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700916 ANDROID_NAMESPACE_TYPE_ISOLATED,
917 nullptr,
918 nullptr);
919
920 ASSERT_DL_NOTNULL(ns);
921
922 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
923
924 std::string content;
925 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
926 ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno);
927
928 android_dlextinfo extinfo;
929 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
930 extinfo.library_fd = tmpfd;
931 extinfo.library_namespace = ns;
932
933 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
934
935 ASSERT_DL_NOTNULL(handle);
936
937 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
938 ASSERT_DL_NOTNULL(taxicab_number);
939 EXPECT_EQ(1729U, *taxicab_number);
940 dlclose(handle);
941}
942
943TEST(dlext, dlopen_ext_use_memfd) {
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700944 const std::string lib_path = GetTestlibRoot() + "/libtest_simple.so";
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700945
946 // create memfd
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700947 int memfd = memfd_create("foobar", MFD_CLOEXEC);
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700948 if (memfd == -1 && errno == ENOSYS) {
949 return;
950 }
951
952 ASSERT_TRUE(memfd != -1) << strerror(errno);
953
954 // Check st.f_type is TMPFS_MAGIC for memfd
955 struct statfs st;
956 ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno);
957 ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type);
958
959 android_namespace_t* ns =
960 android_create_namespace("testing-memfd",
961 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700962 GetTestlibRoot().c_str(),
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -0700963 ANDROID_NAMESPACE_TYPE_ISOLATED,
964 nullptr,
965 nullptr);
966
967 ASSERT_DL_NOTNULL(ns);
968
969 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
970
971 // read file into memfd backed one.
972 std::string content;
973 ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
974 ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno);
975
976 android_dlextinfo extinfo;
977 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
978 extinfo.library_fd = memfd;
979 extinfo.library_namespace = ns;
980
981 void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
982
983 ASSERT_DL_NOTNULL(handle);
984
985 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
986 ASSERT_DL_NOTNULL(taxicab_number);
987 EXPECT_EQ(1729U, *taxicab_number);
988 dlclose(handle);
989}
990
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800991TEST(dlext, ns_symbol_visibilty_one_namespace) {
992 static const char* root_lib = "libnstest_root.so";
993 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
994
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700995 const std::string ns_search_path = GetTestlibRoot() + "/public_namespace_libs:" +
996 GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800997
998 android_namespace_t* ns =
999 android_create_namespace("one",
1000 nullptr,
1001 ns_search_path.c_str(),
1002 ANDROID_NAMESPACE_TYPE_ISOLATED,
1003 nullptr,
1004 nullptr);
1005
1006 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1007
1008 android_dlextinfo extinfo;
1009 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1010 extinfo.library_namespace = ns;
1011
1012 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1013 ASSERT_TRUE(handle != nullptr) << dlerror();
1014
1015 typedef const char* (*fn_t)();
1016
1017 // Check that relocation worked correctly
1018 fn_t ns_get_internal_extern_string =
1019 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
1020 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
1021 ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string());
1022
1023 fn_t internal_extern_string_fn =
1024 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
1025 ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror();
1026 ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn());
1027}
1028
1029TEST(dlext, ns_symbol_visibilty_between_namespaces) {
1030 static const char* root_lib = "libnstest_root.so";
1031 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1032
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001033 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1034 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001035
1036 android_namespace_t* ns_public =
1037 android_create_namespace("public",
1038 nullptr,
1039 public_ns_search_path.c_str(),
1040 ANDROID_NAMESPACE_TYPE_ISOLATED,
1041 nullptr,
1042 nullptr);
1043
1044 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1045
1046 android_namespace_t* ns_private =
1047 android_create_namespace("private",
1048 nullptr,
1049 private_ns_search_path.c_str(),
1050 ANDROID_NAMESPACE_TYPE_ISOLATED,
1051 nullptr,
1052 nullptr);
1053
1054 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
1055 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1056
1057 android_dlextinfo extinfo;
1058 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1059 extinfo.library_namespace = ns_private;
1060
1061 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1062 ASSERT_TRUE(handle != nullptr) << dlerror();
1063
1064 typedef const char* (*fn_t)();
1065
1066 // Check that relocation worked correctly
1067 fn_t ns_get_internal_extern_string =
1068 reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string"));
1069 ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror();
1070 ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) <<
1071 "ns_get_internal_extern_string() expected to return null but returns \"" <<
1072 ns_get_internal_extern_string() << "\"";
1073
1074 fn_t internal_extern_string_fn =
1075 reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string"));
1076 ASSERT_TRUE(internal_extern_string_fn == nullptr);
1077 ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror());
1078}
1079
1080TEST(dlext, ns_unload_between_namespaces) {
1081 static const char* root_lib = "libnstest_root.so";
1082 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1083
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001084 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1085 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001086
1087 android_namespace_t* ns_public =
1088 android_create_namespace("public",
1089 nullptr,
1090 public_ns_search_path.c_str(),
1091 ANDROID_NAMESPACE_TYPE_ISOLATED,
1092 nullptr,
1093 nullptr);
1094
1095 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1096
1097 android_namespace_t* ns_private =
1098 android_create_namespace("private",
1099 nullptr,
1100 private_ns_search_path.c_str(),
1101 ANDROID_NAMESPACE_TYPE_ISOLATED,
1102 nullptr,
1103 nullptr);
1104
1105 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror();
1106 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1107
1108 android_dlextinfo extinfo;
1109 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1110 extinfo.library_namespace = ns_private;
1111
1112 void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1113 ASSERT_TRUE(handle != nullptr) << dlerror();
1114
1115 dlclose(handle);
1116 // Check that root_lib was unloaded
1117 handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1118 ASSERT_TRUE(handle == nullptr);
1119 ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib +
1120 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1121
1122 // Check that shared library was unloaded in public ns
1123 extinfo.library_namespace = ns_public;
1124 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1125 ASSERT_TRUE(handle == nullptr);
1126 ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib +
1127 "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
1128}
1129
dimitry965d06d2017-11-28 16:03:07 +01001130TEST(dlext, ns_unload_between_namespaces_missing_symbol_direct) {
1131 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1132
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001133 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1134 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
dimitry965d06d2017-11-28 16:03:07 +01001135
1136 android_namespace_t* ns_public =
1137 android_create_namespace("public",
1138 nullptr,
1139 public_ns_search_path.c_str(),
1140 ANDROID_NAMESPACE_TYPE_ISOLATED,
1141 nullptr,
1142 nullptr);
1143
1144 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1145
1146 android_namespace_t* ns_private =
1147 android_create_namespace("private",
1148 nullptr,
1149 private_ns_search_path.c_str(),
1150 ANDROID_NAMESPACE_TYPE_ISOLATED,
1151 nullptr,
1152 nullptr);
1153
1154 ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, "libtest_missing_symbol.so")) << dlerror();
1155 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1156
1157 android_dlextinfo extinfo;
1158 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1159 extinfo.library_namespace = ns_private;
1160
1161 void* handle = android_dlopen_ext((public_ns_search_path + "/libtest_missing_symbol.so").c_str(),
1162 RTLD_NOW,
1163 &extinfo);
1164 ASSERT_TRUE(handle == nullptr);
1165 ASSERT_EQ(std::string("dlopen failed: cannot locate symbol \"dlopen_testlib_missing_symbol\" referenced by \"") +
1166 public_ns_search_path + "/libtest_missing_symbol.so\"...",
1167 dlerror());
1168}
1169
1170TEST(dlext, ns_unload_between_namespaces_missing_symbol_indirect) {
1171 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1172
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001173 const std::string public_ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
1174 const std::string private_ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
dimitry965d06d2017-11-28 16:03:07 +01001175
1176 android_namespace_t* ns_public =
1177 android_create_namespace("public",
1178 nullptr,
1179 public_ns_search_path.c_str(),
1180 ANDROID_NAMESPACE_TYPE_ISOLATED,
1181 nullptr,
1182 nullptr);
1183
1184 ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror();
1185
1186 android_namespace_t* ns_private =
1187 android_create_namespace("private",
1188 nullptr,
1189 private_ns_search_path.c_str(),
1190 ANDROID_NAMESPACE_TYPE_ISOLATED,
1191 nullptr,
1192 nullptr);
1193
1194 ASSERT_TRUE(android_link_namespaces(ns_private,
1195 ns_public,
1196 "libnstest_public.so:libtest_missing_symbol_child_public.so")
1197 ) << dlerror();
1198 ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror();
1199
1200 android_dlextinfo extinfo;
1201 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1202 extinfo.library_namespace = ns_private;
1203
1204 void* handle = android_dlopen_ext("libtest_missing_symbol_root.so", RTLD_NOW, &extinfo);
1205 ASSERT_TRUE(handle == nullptr);
1206 ASSERT_EQ(std::string("dlopen failed: cannot locate symbol \"dlopen_testlib_missing_symbol\" referenced by \"") +
1207 private_ns_search_path + "/libtest_missing_symbol_root.so\"...",
1208 dlerror());
1209}
1210
Jiyong Park37b91af2017-05-05 22:07:05 +09001211TEST(dlext, ns_greylist_enabled) {
Dimitry Ivanov18623142017-02-21 13:41:08 -08001212 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1213
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001214 const std::string ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Dimitry Ivanov18623142017-02-21 13:41:08 -08001215
1216 android_namespace_t* ns =
1217 android_create_namespace("namespace",
1218 nullptr,
1219 ns_search_path.c_str(),
Jiyong Park37b91af2017-05-05 22:07:05 +09001220 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED,
Dimitry Ivanov18623142017-02-21 13:41:08 -08001221 nullptr,
1222 nullptr);
1223
1224 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1225
1226 android_dlextinfo extinfo;
1227 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1228 extinfo.library_namespace = ns;
1229
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001230 // An app targeting M can open libnativehelper.so because it's on the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001231 android_set_application_target_sdk_version(__ANDROID_API_M__);
1232 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1233 ASSERT_TRUE(handle != nullptr) << dlerror();
1234
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001235 // Check that loader did not load another copy of libdl.so while loading greylisted library.
1236 void* dlsym_ptr = dlsym(handle, "dlsym");
1237 ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror();
1238 ASSERT_EQ(&dlsym, dlsym_ptr);
1239
Dimitry Ivanov18623142017-02-21 13:41:08 -08001240 dlclose(handle);
1241
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001242 // An app targeting N no longer has the greylist.
Dimitry Ivanov18623142017-02-21 13:41:08 -08001243 android_set_application_target_sdk_version(__ANDROID_API_N__);
1244 handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1245 ASSERT_TRUE(handle == nullptr);
1246 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1247}
1248
Jiyong Park37b91af2017-05-05 22:07:05 +09001249TEST(dlext, ns_greylist_disabled_by_default) {
1250 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1251
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001252 const std::string ns_search_path = GetTestlibRoot() + "/private_namespace_libs";
Jiyong Park37b91af2017-05-05 22:07:05 +09001253
1254 android_namespace_t* ns =
1255 android_create_namespace("namespace",
1256 nullptr,
1257 ns_search_path.c_str(),
1258 ANDROID_NAMESPACE_TYPE_ISOLATED,
1259 nullptr,
1260 nullptr);
1261
1262 ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
1263
1264 android_dlextinfo extinfo;
1265 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1266 extinfo.library_namespace = ns;
1267
1268 android_set_application_target_sdk_version(__ANDROID_API_M__);
1269 void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
1270 ASSERT_TRUE(handle == nullptr);
1271 ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
1272}
1273
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001274TEST(dlext, ns_cyclic_namespaces) {
1275 // Test that ns1->ns2->ns1 link does not break the loader
1276 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1277 std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so";
1278
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001279 const std::string ns_search_path = GetTestlibRoot() + "/public_namespace_libs";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001280
1281 android_namespace_t* ns1 =
1282 android_create_namespace("ns1",
1283 nullptr,
1284 ns_search_path.c_str(),
1285 ANDROID_NAMESPACE_TYPE_ISOLATED,
1286 nullptr,
1287 nullptr);
1288
1289 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
1290
1291 android_namespace_t* ns2 =
1292 android_create_namespace("ns1",
1293 nullptr,
1294 ns_search_path.c_str(),
1295 ANDROID_NAMESPACE_TYPE_ISOLATED,
1296 nullptr,
1297 nullptr);
1298
1299 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
1300
1301 ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror();
1302 ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror();
1303
1304 android_dlextinfo extinfo;
1305 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1306 extinfo.library_namespace = ns1;
1307
1308 void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo);
1309 ASSERT_TRUE(handle == nullptr);
1310 ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror());
1311}
1312
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001313TEST(dlext, ns_isolated) {
1314 static const char* root_lib = "libnstest_root_not_isolated.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001315 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001316
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001317 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001318 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001319 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1320
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08001321 android_set_application_target_sdk_version(42U); // something > 23
1322
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001323 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001324
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001325 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001326 android_create_namespace("private",
1327 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001328 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001329 ANDROID_NAMESPACE_TYPE_REGULAR,
1330 nullptr,
1331 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001332 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001333 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001334
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001335 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001336 android_create_namespace("private_isolated1",
1337 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001338 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001339 ANDROID_NAMESPACE_TYPE_ISOLATED,
1340 nullptr,
1341 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001342 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001343 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001344
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001345 android_namespace_t* ns_isolated2 =
1346 android_create_namespace("private_isolated2",
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001347 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001348 nullptr,
1349 ANDROID_NAMESPACE_TYPE_ISOLATED,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001350 GetTestlibRoot().c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001351 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001352 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001353 ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001354
1355 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
1356 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1357
1358 std::string lib_private_external_path =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001359 GetTestlibRoot() + "/private_namespace_libs_external/libnstest_private_external.so";
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001360
1361 // Load lib_private_external_path to default namespace
1362 // (it should remain invisible for the isolated namespaces after this)
1363 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
1364 ASSERT_TRUE(handle != nullptr) << dlerror();
1365
1366 android_dlextinfo extinfo;
1367 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1368 extinfo.library_namespace = ns_not_isolated;
1369
1370 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1371 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1372
1373 extinfo.library_namespace = ns_isolated;
1374
1375 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1376 ASSERT_TRUE(handle2 == nullptr);
1377 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1378
1379 // Check dlopen by absolute path
1380 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1381 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001382 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001383 " or dlopened by \"" + android::base::GetExecutablePath() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001384 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001385
1386 extinfo.library_namespace = ns_isolated2;
1387
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001388 // this should work because isolation_path for private_isolated2 includes GetTestlibRoot()
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001389 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001390 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1391 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001392
1393 // Check dlopen by absolute path
1394 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08001395 ASSERT_TRUE(handle2 != nullptr) << dlerror();
1396 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001397
1398 typedef const char* (*fn_t)();
1399 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1400 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1401
1402 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1403
1404 fn_t ns_get_private_extern_string =
1405 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1406 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1407
1408 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1409
1410 fn_t ns_get_public_extern_string =
1411 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1412 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1413
1414 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1415
1416 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1417 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1418
1419 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1420
1421 dlclose(handle1);
1422}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001423
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001424TEST(dlext, ns_shared) {
1425 static const char* root_lib = "libnstest_root_not_isolated.so";
1426 static const char* root_lib_isolated = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001427
1428 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001429
Jiyong Park917d34a2017-08-31 14:07:13 +09001430 // create a parent namespace to use instead of the default namespace. This is
1431 // to make this test be independent from the configuration of the default
1432 // namespace.
1433 android_namespace_t* ns_parent =
1434 android_create_namespace("parent",
1435 nullptr,
1436 nullptr,
1437 ANDROID_NAMESPACE_TYPE_REGULAR,
1438 nullptr,
1439 nullptr);
1440 ASSERT_TRUE(ns_parent != nullptr) << dlerror();
1441 ASSERT_TRUE(android_link_namespaces(ns_parent, nullptr, g_core_shared_libs.c_str())) << dlerror();
1442
1443 android_dlextinfo extinfo;
1444 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1445 extinfo.library_namespace = ns_parent;
1446
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001447 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Jiyong Park917d34a2017-08-31 14:07:13 +09001448 void* handle_public = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001449 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1450
1451 android_set_application_target_sdk_version(42U); // something > 23
1452
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001453 ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001454
Jiyong Park917d34a2017-08-31 14:07:13 +09001455 // preload this library to the parent namespace to check if it
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001456 // is shared later on.
1457 void* handle_dlopened =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001458 android_dlopen_ext((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001459 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1460
Jiyong Park917d34a2017-08-31 14:07:13 +09001461 // create two child namespaces of 'ns_parent'. One with regular, the other
1462 // with isolated & shared.
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001463 android_namespace_t* ns_not_isolated =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001464 android_create_namespace("private",
1465 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001466 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001467 ANDROID_NAMESPACE_TYPE_REGULAR,
1468 nullptr,
Jiyong Park917d34a2017-08-31 14:07:13 +09001469 ns_parent);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001470 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
Jiyong Park917d34a2017-08-31 14:07:13 +09001471 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, ns_parent, g_public_lib)) << dlerror();
1472 ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001473
1474 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001475 android_create_namespace("private_isolated_shared",
1476 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001477 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001478 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001479 nullptr,
Jiyong Park917d34a2017-08-31 14:07:13 +09001480 ns_parent);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001481 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Jiyong Park917d34a2017-08-31 14:07:13 +09001482 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, ns_parent, g_public_lib)) << dlerror();
1483 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001484
Jiyong Park917d34a2017-08-31 14:07:13 +09001485 ASSERT_TRUE(android_dlopen_ext(root_lib, RTLD_NOW, &extinfo) == nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001486 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
1487
1488 std::string lib_private_external_path =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001489 GetTestlibRoot() + "/private_namespace_libs_external/libnstest_private_external.so";
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001490
Jiyong Park917d34a2017-08-31 14:07:13 +09001491 // Load lib_private_external_path to the parent namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001492 // (it should remain invisible for the isolated namespaces after this)
Jiyong Park917d34a2017-08-31 14:07:13 +09001493 void* handle = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001494 ASSERT_TRUE(handle != nullptr) << dlerror();
1495
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001496 extinfo.library_namespace = ns_not_isolated;
1497
1498 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1499 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1500
1501 extinfo.library_namespace = ns_isolated_shared;
1502
1503 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1504 ASSERT_TRUE(handle2 == nullptr);
1505 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
1506
1507 // Check dlopen by absolute path
1508 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
1509 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001510 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001511 " or dlopened by \"" + android::base::GetExecutablePath() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001512 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08001513
1514 // load libnstest_root.so to shared namespace in order to check that everything is different
1515 // except shared libnstest_dlopened.so
1516
1517 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
1518
1519 typedef const char* (*fn_t)();
1520 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
1521 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
1522 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
1523 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
1524
1525 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
1526 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
1527 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
1528
1529 fn_t ns_get_private_extern_string =
1530 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
1531 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
1532 fn_t ns_get_private_extern_string_shared =
1533 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
1534 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
1535
1536 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
1537 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
1538 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
1539
1540 fn_t ns_get_public_extern_string =
1541 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
1542 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
1543 fn_t ns_get_public_extern_string_shared =
1544 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
1545 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
1546
1547 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
1548 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
1549 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
1550
1551 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
1552 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
1553 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
1554 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
1555 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
1556 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
1557
1558 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
1559 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
1560 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
1561 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
1562 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
1563
1564 dlclose(handle1);
1565 dlclose(handle2);
1566}
1567
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001568TEST(dlext, ns_shared_links_and_paths) {
1569 // Create parent namespace (isolated, not shared)
1570 android_namespace_t* ns_isolated =
1571 android_create_namespace("private_isolated",
1572 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001573 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001574 ANDROID_NAMESPACE_TYPE_ISOLATED,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001575 (GetTestlibRoot() + "/public_namespace_libs").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001576 nullptr);
1577 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
1578 ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror();
1579
1580 // Create shared namespace with ns_isolated parent
1581 android_namespace_t* ns_shared =
1582 android_create_namespace("private_shared",
1583 nullptr,
1584 nullptr,
1585 ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED,
1586 nullptr,
1587 ns_isolated);
1588 ASSERT_TRUE(ns_shared != nullptr) << dlerror();
1589
1590 // 1. Load a library in ns_shared to check that it has inherited
1591 // search path and the link to the default namespace.
1592 android_dlextinfo extinfo;
1593 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1594 extinfo.library_namespace = ns_shared;
1595
1596 {
1597 void* handle = android_dlopen_ext("libnstest_private.so", RTLD_NOW, &extinfo);
1598 ASSERT_TRUE(handle != nullptr) << dlerror();
1599 const char** ns_private_extern_string = static_cast<const char**>(dlsym(handle, "g_private_extern_string"));
1600 ASSERT_TRUE(ns_private_extern_string != nullptr) << dlerror();
1601 ASSERT_STREQ("This string is from private namespace", *ns_private_extern_string);
1602
1603 dlclose(handle);
1604 }
1605 // 2. Load another test library by absolute path to check that
1606 // it has inherited permitted_when_isolated_path
1607 {
1608 void* handle = android_dlopen_ext(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001609 (GetTestlibRoot() + "/public_namespace_libs/libnstest_public.so").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001610 RTLD_NOW,
1611 &extinfo);
1612
1613 ASSERT_TRUE(handle != nullptr) << dlerror();
1614 const char** ns_public_extern_string = static_cast<const char**>(dlsym(handle, "g_public_extern_string"));
1615 ASSERT_TRUE(ns_public_extern_string != nullptr) << dlerror();
1616 ASSERT_STREQ("This string is from public namespace", *ns_public_extern_string);
1617
1618 dlclose(handle);
1619 }
1620
1621 // 3. Check that it is still isolated.
1622 {
1623 void* handle = android_dlopen_ext(
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001624 (GetTestlibRoot() + "/libtest_empty.so").c_str(),
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07001625 RTLD_NOW,
1626 &extinfo);
1627
1628 ASSERT_TRUE(handle == nullptr);
1629 }
1630}
1631
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001632TEST(dlext, ns_shared_dlclose) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001633 android_set_application_target_sdk_version(42U); // something > 23
1634
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001635 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001636
1637 // preload this library to the default namespace to check if it
1638 // is shared later on.
1639 void* handle_dlopened =
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001640 dlopen((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001641 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
1642
1643 android_namespace_t* ns_isolated_shared =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001644 android_create_namespace("private_isolated_shared",
1645 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001646 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001647 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001648 nullptr,
1649 nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001650 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001651 ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001652
1653 // Check if "libnstest_dlopened.so" is loaded (and the same)
1654 android_dlextinfo extinfo;
1655 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1656 extinfo.library_namespace = ns_isolated_shared;
1657
1658 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1659 ASSERT_TRUE(handle != nullptr) << dlerror();
1660 ASSERT_TRUE(handle == handle_dlopened);
1661 dlclose(handle);
1662 dlclose(handle_dlopened);
1663
1664 // And now check that the library cannot be found by soname (and is no longer loaded)
1665 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1666 ASSERT_TRUE(handle == nullptr)
1667 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1668
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001669 handle = android_dlopen_ext((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001670 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1671 ASSERT_TRUE(handle == nullptr)
1672 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1673
1674 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1675 ASSERT_TRUE(handle == nullptr)
1676 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1677
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001678 handle = dlopen((GetTestlibRoot() + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001679 RTLD_NOW | RTLD_NOLOAD);
1680 ASSERT_TRUE(handle == nullptr)
1681 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1682
1683 // Now lets see if the soinfo area gets reused in the wrong way:
1684 // load a library to default namespace.
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001685 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001686 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1687 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1688
1689 // try to find it in shared namespace
1690 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1691 ASSERT_TRUE(handle == nullptr)
1692 << "Error: " << g_public_lib << " is accessible in shared namespace";
1693}
1694
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001695TEST(dlext, ns_isolated_rtld_global) {
1696 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001697 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001698
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001699 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs";
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001700
1701 android_namespace_t* ns1 =
1702 android_create_namespace("isolated1",
1703 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001704 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001705 ANDROID_NAMESPACE_TYPE_ISOLATED,
1706 lib_public_path.c_str(),
1707 nullptr);
1708 ASSERT_TRUE(ns1 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001709 ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001710
1711 android_namespace_t* ns2 =
1712 android_create_namespace("isolated2",
1713 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001714 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001715 ANDROID_NAMESPACE_TYPE_ISOLATED,
1716 lib_public_path.c_str(),
1717 nullptr);
1718 ASSERT_TRUE(ns2 != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001719 ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001720
1721 android_dlextinfo extinfo;
1722 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1723 extinfo.library_namespace = ns1;
1724
1725 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1726 RTLD_GLOBAL,
1727 &extinfo);
1728
1729 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1730
1731 android_namespace_t* ns1_child =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001732 android_create_namespace("isolated1_child",
1733 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001734 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001735 ANDROID_NAMESPACE_TYPE_ISOLATED,
1736 nullptr,
1737 ns1);
1738
1739 ASSERT_TRUE(ns1_child != nullptr) << dlerror();
1740 ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror();
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001741
1742 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1743 // attempt to use ns2 should result in dlerror()
1744
1745 // Check ns1_child first.
1746 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1747 extinfo.library_namespace = ns1_child;
1748
1749 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1750 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1751
1752 // now ns1
1753 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1754 extinfo.library_namespace = ns1;
1755
1756 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1757 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1758
1759 // and ns2 should fail
1760 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1761 extinfo.library_namespace = ns2;
1762
1763 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1764 ASSERT_TRUE(handle1 == nullptr);
1765 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1766}
1767
dimitry8db36a52017-10-23 15:10:10 +02001768TEST(dlext, ns_inaccessible_error_message) {
1769 // We set up 2 namespaces (a and b) and link a->b with a shared library
1770 // libtestshared.so. Then try to dlopen different library with the same
1771 // name from in namespace a. Note that library should not be accessible
1772 // in either namespace but since it's soname is in the list of shared libs
1773 // the linker will attempt to find it in linked namespace.
1774 //
1775 // Check the error message and make sure it mentions correct namespace name.
1776 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1777
1778 android_namespace_t* ns_a =
1779 android_create_namespace("ns_a",
1780 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001781 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
dimitry8db36a52017-10-23 15:10:10 +02001782 ANDROID_NAMESPACE_TYPE_ISOLATED,
1783 nullptr,
1784 nullptr);
1785 ASSERT_TRUE(ns_a != nullptr) << dlerror();
1786 ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
1787
1788 android_namespace_t* ns_b =
1789 android_create_namespace("ns_b",
1790 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001791 GetTestlibRoot().c_str(),
dimitry8db36a52017-10-23 15:10:10 +02001792 ANDROID_NAMESPACE_TYPE_ISOLATED,
1793 nullptr,
1794 nullptr);
1795 ASSERT_TRUE(ns_b != nullptr) << dlerror();
1796 ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
1797
1798 ASSERT_TRUE(android_link_namespaces(ns_a, ns_b, "libtestshared.so")) << dlerror();
1799
1800 android_dlextinfo extinfo;
1801 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1802 extinfo.library_namespace = ns_a;
1803
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001804 std::string library_path = GetTestlibRoot() + "/inaccessible_libs/libtestshared.so";
dimitry8db36a52017-10-23 15:10:10 +02001805
1806 void* handle = android_dlopen_ext(library_path.c_str(), RTLD_NOW, &extinfo);
1807 ASSERT_TRUE(handle == nullptr);
1808 std::string expected_dlerror =
1809 android::base::StringPrintf("dlopen failed: library \"%s\" needed or dlopened by \"%s\""
1810 " is not accessible for the namespace \"ns_a\"",
1811 library_path.c_str(),
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001812 android::base::GetExecutablePath().c_str());
dimitry8db36a52017-10-23 15:10:10 +02001813 ASSERT_EQ(expected_dlerror, dlerror());
1814}
1815
dimitry321476a2018-01-29 15:32:37 +01001816extern "C" bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
1817 android_namespace_t* namespace_to);
1818
Logan Chien9ee45912018-01-18 12:05:09 +08001819TEST(dlext, ns_link_namespaces_invalid_arguments) {
1820 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1821
1822 android_namespace_t* ns =
1823 android_create_namespace("private",
1824 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001825 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001826 ANDROID_NAMESPACE_TYPE_REGULAR,
1827 nullptr,
1828 nullptr);
1829 ASSERT_TRUE(ns != nullptr) << dlerror();
1830
1831 // Test android_link_namespaces()
1832 ASSERT_FALSE(android_link_namespaces(nullptr, nullptr, "libc.so"));
1833 ASSERT_STREQ("android_link_namespaces failed: error linking namespaces: namespace_from is null.",
1834 dlerror());
1835
1836 ASSERT_FALSE(android_link_namespaces(ns, nullptr, nullptr));
1837 ASSERT_STREQ("android_link_namespaces failed: "
1838 "error linking namespaces \"private\"->\"(default)\": "
1839 "the list of shared libraries is empty.", dlerror());
1840
1841 ASSERT_FALSE(android_link_namespaces(ns, nullptr, ""));
1842 ASSERT_STREQ("android_link_namespaces failed: "
1843 "error linking namespaces \"private\"->\"(default)\": "
1844 "the list of shared libraries is empty.", dlerror());
1845
dimitry321476a2018-01-29 15:32:37 +01001846 // Test __loader_android_link_namespaces_all_libs()
1847 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(nullptr, nullptr));
Logan Chien9ee45912018-01-18 12:05:09 +08001848 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1849 "error linking namespaces: namespace_from is null.", dlerror());
1850
dimitry321476a2018-01-29 15:32:37 +01001851 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(nullptr, ns));
Logan Chien9ee45912018-01-18 12:05:09 +08001852 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1853 "error linking namespaces: namespace_from is null.", dlerror());
1854
dimitry321476a2018-01-29 15:32:37 +01001855 ASSERT_FALSE(__loader_android_link_namespaces_all_libs(ns, nullptr));
Logan Chien9ee45912018-01-18 12:05:09 +08001856 ASSERT_STREQ("android_link_namespaces_all_libs failed: "
1857 "error linking namespaces: namespace_to is null.", dlerror());
1858}
1859
1860TEST(dlext, ns_allow_all_shared_libs) {
1861 ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
1862
1863 android_namespace_t* ns_a =
1864 android_create_namespace("ns_a",
1865 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001866 (GetTestlibRoot() + "/ns_a").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001867 ANDROID_NAMESPACE_TYPE_ISOLATED,
1868 nullptr,
1869 nullptr);
1870 ASSERT_TRUE(ns_a != nullptr) << dlerror();
1871 ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
1872
1873 android_namespace_t* ns_b =
1874 android_create_namespace("ns_b",
1875 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001876 (GetTestlibRoot() + "/ns_b").c_str(),
Logan Chien9ee45912018-01-18 12:05:09 +08001877 ANDROID_NAMESPACE_TYPE_ISOLATED,
1878 nullptr,
1879 nullptr);
1880 ASSERT_TRUE(ns_b != nullptr) << dlerror();
1881 ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
1882
1883 ASSERT_TRUE(android_link_namespaces(ns_b, ns_a, "libnstest_ns_a_public1.so")) << dlerror();
dimitry321476a2018-01-29 15:32:37 +01001884 ASSERT_TRUE(__loader_android_link_namespaces_all_libs(ns_a, ns_b)) << dlerror();
Logan Chien9ee45912018-01-18 12:05:09 +08001885
1886 // Load libs with android_dlopen_ext() from namespace b
1887 android_dlextinfo extinfo;
1888 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1889 extinfo.library_namespace = ns_b;
1890
1891 void* ns_b_handle1 = android_dlopen_ext("libnstest_ns_a_public1.so", RTLD_NOW, &extinfo);
1892 ASSERT_TRUE(ns_b_handle1 != nullptr) << dlerror();
1893
1894 void* ns_b_handle1_internal =
1895 android_dlopen_ext("libnstest_ns_a_public1_internal.so", RTLD_NOW, &extinfo);
1896 ASSERT_TRUE(ns_b_handle1_internal == nullptr);
1897
1898 void* ns_b_handle2 = android_dlopen_ext("libnstest_ns_b_public2.so", RTLD_NOW, &extinfo);
1899 ASSERT_TRUE(ns_b_handle2 != nullptr) << dlerror();
1900
1901 void* ns_b_handle3 = android_dlopen_ext("libnstest_ns_b_public3.so", RTLD_NOW, &extinfo);
1902 ASSERT_TRUE(ns_b_handle3 != nullptr) << dlerror();
1903
1904 // Load libs with android_dlopen_ext() from namespace a
1905 extinfo.library_namespace = ns_a;
1906
1907 void* ns_a_handle1 = android_dlopen_ext("libnstest_ns_a_public1.so", RTLD_NOW, &extinfo);
1908 ASSERT_TRUE(ns_a_handle1 != nullptr) << dlerror();
1909
1910 void* ns_a_handle1_internal =
1911 android_dlopen_ext("libnstest_ns_a_public1_internal.so", RTLD_NOW, &extinfo);
1912 ASSERT_TRUE(ns_a_handle1_internal != nullptr) << dlerror();
1913
1914 void* ns_a_handle2 = android_dlopen_ext("libnstest_ns_b_public2.so", RTLD_NOW, &extinfo);
1915 ASSERT_TRUE(ns_a_handle2 != nullptr) << dlerror();
1916
1917 void* ns_a_handle3 = android_dlopen_ext("libnstest_ns_b_public3.so", RTLD_NOW, &extinfo);
1918 ASSERT_TRUE(ns_a_handle3 != nullptr) << dlerror();
1919
1920 // Compare the dlopen handle
1921 ASSERT_EQ(ns_b_handle1, ns_a_handle1);
1922 ASSERT_EQ(ns_b_handle2, ns_a_handle2);
1923 ASSERT_EQ(ns_b_handle3, ns_a_handle3);
1924
1925 // Close libs
1926 dlclose(ns_b_handle1);
1927 dlclose(ns_b_handle2);
1928 dlclose(ns_b_handle3);
1929
1930 dlclose(ns_a_handle1);
1931 dlclose(ns_a_handle1_internal);
1932 dlclose(ns_a_handle2);
1933 dlclose(ns_a_handle3);
1934}
1935
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001936TEST(dlext, ns_anonymous) {
1937 static const char* root_lib = "libnstest_root.so";
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001938 std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001939
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001940 const std::string lib_public_path = GetTestlibRoot() + "/public_namespace_libs/" + g_public_lib;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001941 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1942
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001943 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1944
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001945 ASSERT_TRUE(
1946 android_init_anonymous_namespace(shared_libs.c_str(),
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001947 (GetTestlibRoot() + "/private_namespace_libs").c_str())
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001948 ) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001949
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001950 android_namespace_t* ns =
1951 android_create_namespace("private",
1952 nullptr,
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001953 (GetTestlibRoot() + "/private_namespace_libs").c_str(),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001954 ANDROID_NAMESPACE_TYPE_REGULAR,
1955 nullptr,
1956 nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001957
1958 ASSERT_TRUE(ns != nullptr) << dlerror();
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001959 ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror();
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001960
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -07001961 std::string private_library_absolute_path = GetTestlibRoot() + "/private_namespace_libs/" + root_lib;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001962
1963 android_dlextinfo extinfo;
1964 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1965 extinfo.library_namespace = ns;
1966
1967 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1968 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1969 ASSERT_TRUE(handle != nullptr) << dlerror();
1970
1971 uintptr_t ns_get_dlopened_string_addr =
1972 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1973 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1974 typedef const char* (*fn_t)();
1975 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1976
1977 std::vector<map_record> maps;
1978 Maps::parse_maps(&maps);
1979
1980 uintptr_t addr_start = 0;
1981 uintptr_t addr_end = 0;
dimitry8eaf28d2017-10-11 10:04:14 +02001982 bool has_executable_segment = false;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001983 std::vector<map_record> maps_to_copy;
1984
1985 for (const auto& rec : maps) {
1986 if (rec.pathname == private_library_absolute_path) {
1987 if (addr_start == 0) {
1988 addr_start = rec.addr_start;
1989 }
1990 addr_end = rec.addr_end;
dimitry8eaf28d2017-10-11 10:04:14 +02001991 has_executable_segment = has_executable_segment || (rec.perms & PROT_EXEC) != 0;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001992
1993 maps_to_copy.push_back(rec);
1994 }
1995 }
1996
1997 // some sanity checks..
1998 ASSERT_TRUE(addr_start > 0);
1999 ASSERT_TRUE(addr_end > 0);
dimitry3b0a5b72018-06-06 11:11:25 +02002000 ASSERT_TRUE(maps_to_copy.size() > 0);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002001 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
2002 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
2003
dimitry8eaf28d2017-10-11 10:04:14 +02002004 if (!has_executable_segment) {
2005 // For some natively bridged environments this code might be missing
2006 // the executable flag. This is because the guest code is not supposed
2007 // to be executed directly and making it non-executable is more secure.
2008 // If this is the case we assume that the first segment is the one that
2009 // has this flag.
2010 ASSERT_TRUE((maps_to_copy[0].perms & PROT_WRITE) == 0);
2011 maps_to_copy[0].perms |= PROT_EXEC;
2012 }
2013
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002014 // copy
2015 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
2016 PROT_NONE, MAP_ANON | MAP_PRIVATE,
2017 -1, 0));
2018 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
2019
2020 for (const auto& rec : maps_to_copy) {
2021 uintptr_t offset = rec.addr_start - addr_start;
2022 size_t size = rec.addr_end - rec.addr_start;
2023 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
2024 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
2025 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
2026 ASSERT_TRUE(map != MAP_FAILED);
2027 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
2028 mprotect(map, size, rec.perms);
2029 }
2030
2031 // call the function copy
2032 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
2033 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
2034 ASSERT_STREQ("This string is from private namespace (dlopened library)",
2035 ns_get_dlopened_string_anon());
2036
2037 // They should belong to different namespaces (private and anonymous)
2038 ASSERT_STREQ("This string is from private namespace (dlopened library)",
2039 ns_get_dlopened_string_private());
2040
2041 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
2042}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002043
2044TEST(dlext, dlopen_handle_value_platform) {
2045 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
2046 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
2047 << "dlopen should return odd value for the handle";
2048 dlclose(handle);
2049}
2050
2051TEST(dlext, dlopen_handle_value_app_compat) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08002052 android_set_application_target_sdk_version(__ANDROID_API_M__);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002053 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
2054 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
2055 << "dlopen should return valid pointer";
2056 dlclose(handle);
2057}