blob: c4e0ac6b839692f2c15d5e19115517b5dc9d68c1 [file] [log] [blame]
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080020#include <elf.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000021#include <errno.h>
22#include <fcntl.h>
Yabin Cui16f7f8d2014-11-04 11:08:05 -080023#include <inttypes.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000024#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000027#include <android/dlext.h>
28#include <sys/mman.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010029#include <sys/types.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000030#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000031
Torne (Richard Coles)26052612014-05-02 14:57:42 +010032#include <pagemap/pagemap.h>
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080033#include <ziparchive/zip_archive.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010034
Yabin Cui294d1e22014-12-07 20:43:37 -080035#include "TemporaryFile.h"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080036#include "utils.h"
Dimitry Ivanov41fd2952016-05-09 17:37:39 -070037#include "dlext_private.h"
Dimitry Ivanov708589f2016-09-19 10:50:28 -070038#include "dlfcn_symlink_support.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000039
40#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070041 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000042
43#define ASSERT_DL_ZERO(i) \
44 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
45
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000046#define ASSERT_NOERROR(i) \
47 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
48
Yabin Cui16f7f8d2014-11-04 11:08:05 -080049#define ASSERT_SUBSTR(needle, haystack) \
50 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
51
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000052
53typedef int (*fn)(void);
54#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010055#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070056constexpr auto LIBSIZE = 1024 * 1024; // how much address space to reserve for it
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000057
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070058#if defined(__LP64__)
Dimitry Ivanova36e59b2016-09-01 11:37:39 -070059#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070060#else
Dimitry Ivanova36e59b2016-09-01 11:37:39 -070061#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070062#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000063
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070064#define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
65#define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
66#define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080067#define LIBZIP_SIMPLE_ZIP "libdir/libatest_simple_zip.so"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070068
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000069class DlExtTest : public ::testing::Test {
70protected:
71 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070072 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000073 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070074 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
75 ASSERT_TRUE(h == nullptr);
76 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
77 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000078 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070079 ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000080 }
81
82 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070083 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000084 ASSERT_DL_ZERO(dlclose(handle_));
85 }
86 }
87
88 void* handle_;
89};
90
91TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070092 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000093 ASSERT_DL_NOTNULL(handle_);
94 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
95 ASSERT_DL_NOTNULL(f);
96 EXPECT_EQ(4, f());
97}
98
99TEST_F(DlExtTest, ExtInfoNoFlags) {
100 android_dlextinfo extinfo;
101 extinfo.flags = 0;
102 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
103 ASSERT_DL_NOTNULL(handle_);
104 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
105 ASSERT_DL_NOTNULL(f);
106 EXPECT_EQ(4, f());
107}
108
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700109TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700110 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700111
112 android_dlextinfo extinfo;
113 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700114 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700115 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700116 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700117 ASSERT_DL_NOTNULL(handle_);
118 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
119 ASSERT_DL_NOTNULL(f);
120 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700121
122 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
123 ASSERT_DL_NOTNULL(taxicab_number);
124 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700125}
126
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700127TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700128 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700129
130 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700131 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700132 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800133
134 // Find the offset of the shared library in the zip.
135 ZipArchiveHandle handle;
136 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
137 ZipEntry zip_entry;
138 ZipString zip_name;
139 zip_name.name = reinterpret_cast<const uint8_t*>(LIBZIP_SIMPLE_ZIP);
140 zip_name.name_length = sizeof(LIBZIP_SIMPLE_ZIP) - 1;
141 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
142 extinfo.library_fd_offset = zip_entry.offset;
143 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700144
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700145 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700146 ASSERT_DL_NOTNULL(handle_);
147
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700148 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
149 ASSERT_DL_NOTNULL(taxicab_number);
150 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700151}
152
153TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700154 // lib_path is relative when $ANDROID_DATA is relative
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700155 std::string lib_path;
156 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700157
158 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700160 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700161 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700162
163 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
164 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700165 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
166
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800167 // Test an address above 2^44, for http://b/18178121 .
168 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700169 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700170 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800171 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
172
173 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
174 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
175 ASSERT_TRUE(handle_ == nullptr);
176 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
177
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700178 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700179 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800180 ASSERT_TRUE(handle_ == nullptr);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700181 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800183 // Check if dlsym works after unsuccessful dlopen().
184 // Supply non-exiting one to make linker visit every soinfo.
185 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
186 ASSERT_TRUE(sym == nullptr);
187
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700188 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700189}
190
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800191TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700192 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700193 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800194 // This offset will not be used, so it doesn't matter.
195 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700196
197 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
198 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700199 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 -0700200}
201
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700202TEST(dlext, android_dlopen_ext_force_load_smoke) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700203 DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
204 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700205 // 1. Open actual file
206 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
207 ASSERT_DL_NOTNULL(handle);
208 // 2. Open link with force_load flag set
209 android_dlextinfo extinfo;
210 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700211 void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700212 ASSERT_DL_NOTNULL(handle2);
213 ASSERT_TRUE(handle != handle2);
214
215 dlclose(handle2);
216 dlclose(handle);
217}
218
219TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700220 DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
221 const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700222 // Check if soname lookup still returns already loaded library
223 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700224 void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700225 ASSERT_DL_NOTNULL(handle);
226
227 android_dlextinfo extinfo;
228 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
229
Dimitry Ivanov708589f2016-09-19 10:50:28 -0700230 // Note that 'libdlext_test.so' is dt_soname for the symlink_name
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700231 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
232
233 ASSERT_DL_NOTNULL(handle2);
234 ASSERT_TRUE(handle == handle2);
235
236 dlclose(handle2);
237 dlclose(handle);
238}
239
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700240TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700241 std::string lib_path;
242 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700243
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700244 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700245 ASSERT_TRUE(handle != nullptr) << dlerror();
246
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700247 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
248 ASSERT_DL_NOTNULL(taxicab_number);
249 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700250
251 dlclose(handle);
252}
253
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700254TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700255 std::string data_path;
256 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
257 const std::string lib_path = data_path + LIBZIPPATH_WITH_RUNPATH;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700258
259 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
260
261 ASSERT_TRUE(handle != nullptr) << dlerror();
262
263 typedef void *(* dlopen_b_fn)();
264 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
265 ASSERT_TRUE(fn != nullptr) << dlerror();
266
267 void *p = fn();
268 ASSERT_TRUE(p != nullptr) << dlerror();
269
270 dlclose(p);
271 dlclose(handle);
272}
273
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700274TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700275 std::string data_path;
276 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
277 const std::string lib_path = data_path + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700278
279 typedef void (*fn_t)(const char*);
280 fn_t android_update_LD_LIBRARY_PATH =
281 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
282
283 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
284
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700285 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700286 ASSERT_TRUE(handle == nullptr);
287
288 android_update_LD_LIBRARY_PATH(lib_path.c_str());
289
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700290 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700291 ASSERT_TRUE(handle != nullptr) << dlerror();
292
293 int (*fn)(void);
294 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
295 ASSERT_TRUE(fn != nullptr);
296 EXPECT_EQ(4, fn());
297
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800298 uint32_t* taxicab_number =
299 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700300 ASSERT_DL_NOTNULL(taxicab_number);
301 EXPECT_EQ(1729U, *taxicab_number);
302
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700303 dlclose(handle);
304}
305
306
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000307TEST_F(DlExtTest, Reserved) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800308 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000309 ASSERT_TRUE(start != MAP_FAILED);
310 android_dlextinfo extinfo;
311 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
312 extinfo.reserved_addr = start;
313 extinfo.reserved_size = LIBSIZE;
314 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
315 ASSERT_DL_NOTNULL(handle_);
316 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
317 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700318 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000319 EXPECT_LT(reinterpret_cast<void*>(f),
320 reinterpret_cast<char*>(start) + LIBSIZE);
321 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800322
323 // Check that after dlclose reserved address space is unmapped (and can be reused)
324 dlclose(handle_);
325 handle_ = nullptr;
326
327 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
328 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000329}
330
331TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800332 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000333 ASSERT_TRUE(start != MAP_FAILED);
334 android_dlextinfo extinfo;
335 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
336 extinfo.reserved_addr = start;
337 extinfo.reserved_size = PAGE_SIZE;
338 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700339 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000340}
341
342TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800343 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000344 ASSERT_TRUE(start != MAP_FAILED);
345 android_dlextinfo extinfo;
346 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
347 extinfo.reserved_addr = start;
348 extinfo.reserved_size = LIBSIZE;
349 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
350 ASSERT_DL_NOTNULL(handle_);
351 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
352 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700353 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000354 EXPECT_LT(reinterpret_cast<void*>(f),
355 reinterpret_cast<char*>(start) + LIBSIZE);
356 EXPECT_EQ(4, f());
357}
358
359TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800360 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000361 ASSERT_TRUE(start != MAP_FAILED);
362 android_dlextinfo extinfo;
363 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
364 extinfo.reserved_addr = start;
365 extinfo.reserved_size = PAGE_SIZE;
366 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
367 ASSERT_DL_NOTNULL(handle_);
368 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
369 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700370 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
371 (reinterpret_cast<void*>(f) >=
372 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000373 EXPECT_EQ(4, f());
374}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000375
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700376TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800377 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700378 ASSERT_TRUE(start != MAP_FAILED);
379 munmap(start, LIBSIZE);
380
381 android_dlextinfo extinfo;
382 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
383 extinfo.reserved_addr = start;
384
385 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
386 ASSERT_DL_NOTNULL(handle_);
387 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
388 ASSERT_DL_NOTNULL(f);
389 EXPECT_GE(reinterpret_cast<void*>(f), start);
390 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
391
392 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800393 dlclose(handle_);
394 handle_ = nullptr;
395
396 // Check that dlclose unmapped the file
397 void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
398 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700399}
400
401TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
402 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
403 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
404 ASSERT_TRUE(start != MAP_FAILED);
405 munmap(start, LIBSIZE + PAGE_SIZE);
406 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
407 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
408 ASSERT_TRUE(new_addr != MAP_FAILED);
409
410 android_dlextinfo extinfo;
411 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
412 extinfo.reserved_addr = start;
413
414 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
415 ASSERT_TRUE(handle_ == nullptr);
416}
417
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100418class DlExtRelroSharingTest : public DlExtTest {
419protected:
420 virtual void SetUp() {
421 DlExtTest::SetUp();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800422 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100423 ASSERT_TRUE(start != MAP_FAILED);
424 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
425 extinfo_.reserved_addr = start;
426 extinfo_.reserved_size = LIBSIZE;
427 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000428 }
429
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100430 virtual void TearDown() {
431 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100432 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000433
Yabin Cui294d1e22014-12-07 20:43:37 -0800434 void CreateRelroFile(const char* lib, const char* relro_file) {
435 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100436 ASSERT_NOERROR(relro_fd);
437
438 pid_t pid = fork();
439 if (pid == 0) {
440 // child process
441 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
442 extinfo_.relro_fd = relro_fd;
443 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700444 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100445 fprintf(stderr, "in child: %s\n", dlerror());
446 exit(1);
447 }
448 exit(0);
449 }
450
451 // continuing in parent
452 ASSERT_NOERROR(close(relro_fd));
453 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800454 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100455
456 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800457 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100458 ASSERT_NOERROR(relro_fd);
459 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
460 extinfo_.relro_fd = relro_fd;
461 }
462
463 void TryUsingRelro(const char* lib) {
464 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
465 ASSERT_DL_NOTNULL(handle_);
466 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
467 ASSERT_DL_NOTNULL(f);
468 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700469
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800470 uint32_t* taxicab_number =
471 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700472 ASSERT_DL_NOTNULL(taxicab_number);
473 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100474 }
475
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100476 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
477
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100478 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100479};
480
481TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800482 TemporaryFile tf; // Use tf to get an unique filename.
483 ASSERT_NOERROR(close(tf.fd));
484
485 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100486 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800487
488 // Use destructor of tf to close and unlink the file.
489 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100490}
491
492TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800493 TemporaryFile tf; // // Use tf to get an unique filename.
494 ASSERT_NOERROR(close(tf.fd));
495
496 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100497 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800498
499 // Use destructor of tf to close and unlink the file.
500 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100501}
502
503TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100504 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000505}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100506
507TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700508 if (geteuid() != 0) {
509 GTEST_LOG_(INFO) << "This test must be run as root.\n";
510 return;
511 }
512
Yabin Cui294d1e22014-12-07 20:43:37 -0800513 TemporaryFile tf; // Use tf to get an unique filename.
514 ASSERT_NOERROR(close(tf.fd));
515
516 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
517
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100518 int pipefd[2];
519 ASSERT_NOERROR(pipe(pipefd));
520
521 size_t without_sharing, with_sharing;
522 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
523 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
524
525 // We expect the sharing to save at least 10% of the total PSS. In practice
526 // it saves 40%+ for this test.
527 size_t expected_size = without_sharing - (without_sharing/10);
528 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800529
530 // Use destructor of tf to close and unlink the file.
531 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100532}
533
534void getPss(pid_t pid, size_t* pss_out) {
535 pm_kernel_t* kernel;
536 ASSERT_EQ(0, pm_kernel_create(&kernel));
537
538 pm_process_t* process;
539 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
540
541 pm_map_t** maps;
542 size_t num_maps;
543 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
544
545 size_t total_pss = 0;
546 for (size_t i = 0; i < num_maps; i++) {
547 pm_memusage_t usage;
548 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
549 total_pss += usage.pss;
550 }
551 *pss_out = total_pss;
552
553 free(maps);
554 pm_process_destroy(process);
555 pm_kernel_destroy(kernel);
556}
557
558void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
559 size_t* pss_out) {
560 const int CHILDREN = 20;
561
562 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800563 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100564 int childpipe[CHILDREN];
565 for (int i=0; i<CHILDREN; ++i) {
566 char read_buf;
567 int child_done_pipe[2], parent_done_pipe[2];
568 ASSERT_NOERROR(pipe(child_done_pipe));
569 ASSERT_NOERROR(pipe(parent_done_pipe));
570
571 pid_t child = fork();
572 if (child == 0) {
573 // close the 'wrong' ends of the pipes in the child
574 close(child_done_pipe[0]);
575 close(parent_done_pipe[1]);
576
577 // open the library
578 void* handle;
579 if (share_relro) {
580 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
581 } else {
582 handle = dlopen(lib, RTLD_NOW);
583 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700584 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100585 fprintf(stderr, "in child: %s\n", dlerror());
586 exit(1);
587 }
588
589 // close write end of child_done_pipe to signal the parent that we're done.
590 close(child_done_pipe[1]);
591
592 // wait for the parent to close parent_done_pipe, then exit
593 read(parent_done_pipe[0], &read_buf, 1);
594 exit(0);
595 }
596
597 ASSERT_NOERROR(child);
598
599 // close the 'wrong' ends of the pipes in the parent
600 close(child_done_pipe[1]);
601 close(parent_done_pipe[0]);
602
603 // wait for the child to be done
604 read(child_done_pipe[0], &read_buf, 1);
605 close(child_done_pipe[0]);
606
607 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800608 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100609 childpipe[i] = parent_done_pipe[1];
610 }
611
612 // Sum the PSS of all the children
613 size_t total_pss = 0;
614 for (int i=0; i<CHILDREN; ++i) {
615 size_t child_pss;
Elliott Hughes33697a02016-01-26 13:04:57 -0800616 ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100617 total_pss += child_pss;
618 }
619 *pss_out = total_pss;
620
621 // Close pipes and wait for children to exit
622 for (int i=0; i<CHILDREN; ++i) {
623 ASSERT_NOERROR(close(childpipe[i]));
624 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800625 for (int i = 0; i < CHILDREN; ++i) {
626 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100627 }
628}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700629
630// Testing namespaces
631static const char* g_public_lib = "libnstest_public.so";
632
633TEST(dlext, ns_smoke) {
634 static const char* root_lib = "libnstest_root.so";
635 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
636
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800637 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
638 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dimitry Ivanov3a6c6b32016-07-13 16:28:20 -0700639 "a library with soname \"libnstest_public.so\" was not found in the "
640 "default namespace",
641 dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700642
Dimitry Ivanov54807612016-04-21 14:57:38 -0700643 ASSERT_FALSE(android_init_namespaces("", nullptr));
644 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
645 "the list of public libraries is empty.", dlerror());
646
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700647 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
648
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800649 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
650 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700651 ASSERT_TRUE(handle_public != nullptr) << dlerror();
652
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800653 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700654
655 // Check that libraries added to public namespace are NODELETE
656 dlclose(handle_public);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800657 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
658 RTLD_NOW | RTLD_NOLOAD);
659
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700660 ASSERT_TRUE(handle_public != nullptr) << dlerror();
661
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800662 android_namespace_t* ns1 =
663 android_create_namespace("private", nullptr,
664 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700665 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700666 ASSERT_TRUE(ns1 != nullptr) << dlerror();
667
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800668 android_namespace_t* ns2 =
669 android_create_namespace("private_isolated", nullptr,
670 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700671 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700672 ASSERT_TRUE(ns2 != nullptr) << dlerror();
673
674 // This should not have affect search path for default namespace:
675 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
676 void* handle = dlopen(g_public_lib, RTLD_NOW);
677 ASSERT_TRUE(handle != nullptr) << dlerror();
678 dlclose(handle);
679
680 android_dlextinfo extinfo;
681 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
682 extinfo.library_namespace = ns1;
683
684 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
685 ASSERT_TRUE(handle1 != nullptr) << dlerror();
686
687 extinfo.library_namespace = ns2;
688 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
689 ASSERT_TRUE(handle2 != nullptr) << dlerror();
690
691 ASSERT_TRUE(handle1 != handle2);
692
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800693 // dlopen for a public library using an absolute path should work for isolated namespaces
694 extinfo.library_namespace = ns2;
695 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
696 ASSERT_TRUE(handle != nullptr) << dlerror();
697 ASSERT_TRUE(handle == handle_public);
698
699 dlclose(handle);
700
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700701 typedef const char* (*fn_t)();
702
703 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
704 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
705 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
706 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
707
708 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
709 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
710
711 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
712
713 fn_t ns_get_private_extern_string1 =
714 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
715 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
716 fn_t ns_get_private_extern_string2 =
717 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
718 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
719
720 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
721 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
722
723 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
724
725 fn_t ns_get_public_extern_string1 =
726 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
727 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
728 fn_t ns_get_public_extern_string2 =
729 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
730 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
731
732 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
733 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
734
735 // and now check that dlopen() does the right thing in terms of preserving namespace
736 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
737 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
738 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
739 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
740
741 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
742 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
743
744 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
745
746 dlclose(handle1);
747
748 // Check if handle2 is still alive (and well)
749 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
750 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
751 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
752 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
753
754 dlclose(handle2);
755}
756
757TEST(dlext, ns_isolated) {
758 static const char* root_lib = "libnstest_root_not_isolated.so";
759 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
760
761 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800762 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
763 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700764 ASSERT_TRUE(handle_public != nullptr) << dlerror();
765
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800766 android_set_application_target_sdk_version(42U); // something > 23
767
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800768 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700769
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800770 android_namespace_t* ns_not_isolated =
771 android_create_namespace("private", nullptr,
772 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700773 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700774 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
775
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800776 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700777 android_create_namespace("private_isolated1",
778 nullptr,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800779 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700780 ANDROID_NAMESPACE_TYPE_ISOLATED,
781 nullptr,
782 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700783 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
784
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800785 android_namespace_t* ns_isolated2 =
786 android_create_namespace("private_isolated2",
787 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700788 nullptr,
789 ANDROID_NAMESPACE_TYPE_ISOLATED,
790 lib_path.c_str(),
791 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700792 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
793
794 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
795 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
796
797 std::string lib_private_external_path =
798 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
799
800 // Load lib_private_external_path to default namespace
801 // (it should remain invisible for the isolated namespaces after this)
802 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
803 ASSERT_TRUE(handle != nullptr) << dlerror();
804
805 android_dlextinfo extinfo;
806 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
807 extinfo.library_namespace = ns_not_isolated;
808
809 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
810 ASSERT_TRUE(handle1 != nullptr) << dlerror();
811
812 extinfo.library_namespace = ns_isolated;
813
814 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
815 ASSERT_TRUE(handle2 == nullptr);
816 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
817
818 // Check dlopen by absolute path
819 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
820 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800821 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700822 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800823 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700824
825 extinfo.library_namespace = ns_isolated2;
826
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800827 // this should work because isolation_path for private_isolated2 includes lib_path
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700828 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800829 ASSERT_TRUE(handle2 != nullptr) << dlerror();
830 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700831
832 // Check dlopen by absolute path
833 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800834 ASSERT_TRUE(handle2 != nullptr) << dlerror();
835 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700836
837 typedef const char* (*fn_t)();
838 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
839 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
840
841 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
842
843 fn_t ns_get_private_extern_string =
844 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
845 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
846
847 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
848
849 fn_t ns_get_public_extern_string =
850 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
851 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
852
853 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
854
855 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
856 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
857
858 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
859
860 dlclose(handle1);
861}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800862
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800863TEST(dlext, ns_shared) {
864 static const char* root_lib = "libnstest_root_not_isolated.so";
865 static const char* root_lib_isolated = "libnstest_root.so";
866 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
867
868 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
869 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
870 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
871 ASSERT_TRUE(handle_public != nullptr) << dlerror();
872
873 android_set_application_target_sdk_version(42U); // something > 23
874
875 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
876
877 // preload this library to the default namespace to check if it
878 // is shared later on.
879 void* handle_dlopened =
880 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
881 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
882
883 android_namespace_t* ns_not_isolated =
884 android_create_namespace("private", nullptr,
885 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700886 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800887 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
888
889 android_namespace_t* ns_isolated_shared =
890 android_create_namespace("private_isolated_shared", nullptr,
891 (lib_path + "/private_namespace_libs").c_str(),
892 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700893 nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800894 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
895
896 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
897 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
898
899 std::string lib_private_external_path =
900 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
901
902 // Load lib_private_external_path to default namespace
903 // (it should remain invisible for the isolated namespaces after this)
904 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
905 ASSERT_TRUE(handle != nullptr) << dlerror();
906
907 android_dlextinfo extinfo;
908 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
909 extinfo.library_namespace = ns_not_isolated;
910
911 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
912 ASSERT_TRUE(handle1 != nullptr) << dlerror();
913
914 extinfo.library_namespace = ns_isolated_shared;
915
916 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
917 ASSERT_TRUE(handle2 == nullptr);
918 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
919
920 // Check dlopen by absolute path
921 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
922 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800923 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700924 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800925 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800926
927 // load libnstest_root.so to shared namespace in order to check that everything is different
928 // except shared libnstest_dlopened.so
929
930 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
931
932 typedef const char* (*fn_t)();
933 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
934 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
935 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
936 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
937
938 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
939 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
940 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
941
942 fn_t ns_get_private_extern_string =
943 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
944 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
945 fn_t ns_get_private_extern_string_shared =
946 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
947 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
948
949 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
950 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
951 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
952
953 fn_t ns_get_public_extern_string =
954 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
955 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
956 fn_t ns_get_public_extern_string_shared =
957 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
958 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
959
960 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
961 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
962 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
963
964 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
965 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
966 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
967 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
968 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
969 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
970
971 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
972 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
973 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
974 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
975 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
976
977 dlclose(handle1);
978 dlclose(handle2);
979}
980
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700981TEST(dlext, ns_shared_dlclose) {
982 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
983
984 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
985
986 android_set_application_target_sdk_version(42U); // something > 23
987
988 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
989
990 // preload this library to the default namespace to check if it
991 // is shared later on.
992 void* handle_dlopened =
993 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
994 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
995
996 android_namespace_t* ns_isolated_shared =
997 android_create_namespace("private_isolated_shared", nullptr,
998 (lib_path + "/private_namespace_libs").c_str(),
999 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001000 nullptr, nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001001 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
1002
1003 // Check if "libnstest_dlopened.so" is loaded (and the same)
1004 android_dlextinfo extinfo;
1005 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1006 extinfo.library_namespace = ns_isolated_shared;
1007
1008 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1009 ASSERT_TRUE(handle != nullptr) << dlerror();
1010 ASSERT_TRUE(handle == handle_dlopened);
1011 dlclose(handle);
1012 dlclose(handle_dlopened);
1013
1014 // And now check that the library cannot be found by soname (and is no longer loaded)
1015 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1016 ASSERT_TRUE(handle == nullptr)
1017 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1018
1019 handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1020 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1021 ASSERT_TRUE(handle == nullptr)
1022 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1023
1024 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1025 ASSERT_TRUE(handle == nullptr)
1026 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1027
1028 handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1029 RTLD_NOW | RTLD_NOLOAD);
1030 ASSERT_TRUE(handle == nullptr)
1031 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1032
1033 // Now lets see if the soinfo area gets reused in the wrong way:
1034 // load a library to default namespace.
1035 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1036 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1037 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1038
1039 // try to find it in shared namespace
1040 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1041 ASSERT_TRUE(handle == nullptr)
1042 << "Error: " << g_public_lib << " is accessible in shared namespace";
1043}
1044
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001045TEST(dlext, ns_isolated_rtld_global) {
1046 static const char* root_lib = "libnstest_root.so";
1047 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
1048
1049 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
1050
1051 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
1052
1053 const std::string lib_public_path = lib_path + "/public_namespace_libs";
1054
1055 android_namespace_t* ns1 =
1056 android_create_namespace("isolated1",
1057 nullptr,
1058 (lib_path + "/private_namespace_libs").c_str(),
1059 ANDROID_NAMESPACE_TYPE_ISOLATED,
1060 lib_public_path.c_str(),
1061 nullptr);
1062 ASSERT_TRUE(ns1 != nullptr) << dlerror();
1063
1064 android_namespace_t* ns2 =
1065 android_create_namespace("isolated2",
1066 nullptr,
1067 (lib_path + "/private_namespace_libs").c_str(),
1068 ANDROID_NAMESPACE_TYPE_ISOLATED,
1069 lib_public_path.c_str(),
1070 nullptr);
1071 ASSERT_TRUE(ns2 != nullptr) << dlerror();
1072
1073 android_dlextinfo extinfo;
1074 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1075 extinfo.library_namespace = ns1;
1076
1077 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1078 RTLD_GLOBAL,
1079 &extinfo);
1080
1081 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1082
1083 android_namespace_t* ns1_child =
1084 android_create_namespace("isolated1_child",
1085 nullptr,
1086 (lib_path + "/private_namespace_libs").c_str(),
1087 ANDROID_NAMESPACE_TYPE_ISOLATED,
1088 nullptr,
1089 ns1);
1090
1091 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1092 // attempt to use ns2 should result in dlerror()
1093
1094 // Check ns1_child first.
1095 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1096 extinfo.library_namespace = ns1_child;
1097
1098 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1099 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1100
1101 // now ns1
1102 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1103 extinfo.library_namespace = ns1;
1104
1105 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1106 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1107
1108 // and ns2 should fail
1109 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1110 extinfo.library_namespace = ns2;
1111
1112 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1113 ASSERT_TRUE(handle1 == nullptr);
1114 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1115}
1116
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001117TEST(dlext, ns_anonymous) {
1118 static const char* root_lib = "libnstest_root.so";
1119 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
1120
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001121 std::string data_path;
1122 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1123 const std::string lib_path = data_path + NATIVE_TESTS_PATH;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001124
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001125 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1126 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1127
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001128 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1129
1130 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
1131 << dlerror();
1132
1133 android_namespace_t* ns = android_create_namespace(
1134 "private", nullptr,
1135 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001136 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001137
1138 ASSERT_TRUE(ns != nullptr) << dlerror();
1139
1140 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
1141
1142 android_dlextinfo extinfo;
1143 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1144 extinfo.library_namespace = ns;
1145
1146 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1147 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1148 ASSERT_TRUE(handle != nullptr) << dlerror();
1149
1150 uintptr_t ns_get_dlopened_string_addr =
1151 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1152 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1153 typedef const char* (*fn_t)();
1154 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1155
1156 std::vector<map_record> maps;
1157 Maps::parse_maps(&maps);
1158
1159 uintptr_t addr_start = 0;
1160 uintptr_t addr_end = 0;
1161 std::vector<map_record> maps_to_copy;
1162
1163 for (const auto& rec : maps) {
1164 if (rec.pathname == private_library_absolute_path) {
1165 if (addr_start == 0) {
1166 addr_start = rec.addr_start;
1167 }
1168 addr_end = rec.addr_end;
1169
1170 maps_to_copy.push_back(rec);
1171 }
1172 }
1173
1174 // some sanity checks..
1175 ASSERT_TRUE(addr_start > 0);
1176 ASSERT_TRUE(addr_end > 0);
1177 ASSERT_EQ(3U, maps_to_copy.size());
1178 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1179 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1180
1181 // copy
1182 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1183 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1184 -1, 0));
1185 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1186
1187 for (const auto& rec : maps_to_copy) {
1188 uintptr_t offset = rec.addr_start - addr_start;
1189 size_t size = rec.addr_end - rec.addr_start;
1190 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1191 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1192 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1193 ASSERT_TRUE(map != MAP_FAILED);
1194 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1195 mprotect(map, size, rec.perms);
1196 }
1197
1198 // call the function copy
1199 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1200 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1201 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1202 ns_get_dlopened_string_anon());
1203
1204 // They should belong to different namespaces (private and anonymous)
1205 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1206 ns_get_dlopened_string_private());
1207
1208 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1209}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001210
1211TEST(dlext, dlopen_handle_value_platform) {
1212 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1213 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1214 << "dlopen should return odd value for the handle";
1215 dlclose(handle);
1216}
1217
1218TEST(dlext, dlopen_handle_value_app_compat) {
1219 android_set_application_target_sdk_version(23);
1220 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1221 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1222 << "dlopen should return valid pointer";
1223 dlclose(handle);
1224}
1225