blob: f597e6188f836daeb2b37ef27cf2c8ba13f27fb5 [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"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000038
39#define ASSERT_DL_NOTNULL(ptr) \
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070040 ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000041
42#define ASSERT_DL_ZERO(i) \
43 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
44
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000045#define ASSERT_NOERROR(i) \
46 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
47
Yabin Cui16f7f8d2014-11-04 11:08:05 -080048#define ASSERT_SUBSTR(needle, haystack) \
49 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
50
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000051
52typedef int (*fn)(void);
53#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010054#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Chih-Hung Hsiehd61ca372016-06-03 10:18:07 -070055constexpr auto LIBSIZE = 1024 * 1024; // how much address space to reserve for it
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000056
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070057#if defined(__LP64__)
Dimitry Ivanova36e59b2016-09-01 11:37:39 -070058#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070059#else
Dimitry Ivanova36e59b2016-09-01 11:37:39 -070060#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070061#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000062
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070063#define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
64#define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
65#define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -080066#define LIBZIP_SIMPLE_ZIP "libdir/libatest_simple_zip.so"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070067
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000068class DlExtTest : public ::testing::Test {
69protected:
70 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070071 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000072 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070073 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
74 ASSERT_TRUE(h == nullptr);
75 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
76 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000077 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070078 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 +000079 }
80
81 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070082 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000083 ASSERT_DL_ZERO(dlclose(handle_));
84 }
85 }
86
87 void* handle_;
88};
89
90TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070091 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000092 ASSERT_DL_NOTNULL(handle_);
93 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
94 ASSERT_DL_NOTNULL(f);
95 EXPECT_EQ(4, f());
96}
97
98TEST_F(DlExtTest, ExtInfoNoFlags) {
99 android_dlextinfo extinfo;
100 extinfo.flags = 0;
101 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
102 ASSERT_DL_NOTNULL(handle_);
103 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
104 ASSERT_DL_NOTNULL(f);
105 EXPECT_EQ(4, f());
106}
107
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700109 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700110
111 android_dlextinfo extinfo;
112 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700113 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700114 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700115 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700116 ASSERT_DL_NOTNULL(handle_);
117 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
118 ASSERT_DL_NOTNULL(f);
119 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700120
121 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
122 ASSERT_DL_NOTNULL(taxicab_number);
123 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700124}
125
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700126TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700127 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700128
129 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700130 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700131 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800132
133 // Find the offset of the shared library in the zip.
134 ZipArchiveHandle handle;
135 ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
136 ZipEntry zip_entry;
137 ZipString zip_name;
138 zip_name.name = reinterpret_cast<const uint8_t*>(LIBZIP_SIMPLE_ZIP);
139 zip_name.name_length = sizeof(LIBZIP_SIMPLE_ZIP) - 1;
140 ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
141 extinfo.library_fd_offset = zip_entry.offset;
142 CloseArchive(handle);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700143
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700144 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700145 ASSERT_DL_NOTNULL(handle_);
146
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700147 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
148 ASSERT_DL_NOTNULL(taxicab_number);
149 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700150}
151
152TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700153 // lib_path is relative when $ANDROID_DATA is relative
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700154 std::string lib_path;
155 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700156
157 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700158 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700159 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700160 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700161
162 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
163 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700164 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
165
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800166 // Test an address above 2^44, for http://b/18178121 .
167 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700168 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700169 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800170 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
171
172 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
173 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
174 ASSERT_TRUE(handle_ == nullptr);
175 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
176
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700177 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700178 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800179 ASSERT_TRUE(handle_ == nullptr);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700180 ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700181
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800182 // Check if dlsym works after unsuccessful dlopen().
183 // Supply non-exiting one to make linker visit every soinfo.
184 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
185 ASSERT_TRUE(sym == nullptr);
186
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700187 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700188}
189
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800190TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700191 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700192 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800193 // This offset will not be used, so it doesn't matter.
194 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700195
196 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
197 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700198 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 -0700199}
200
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700201TEST(dlext, android_dlopen_ext_force_load_smoke) {
202 // 1. Open actual file
203 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
204 ASSERT_DL_NOTNULL(handle);
205 // 2. Open link with force_load flag set
206 android_dlextinfo extinfo;
207 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
208 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
209 ASSERT_DL_NOTNULL(handle2);
210 ASSERT_TRUE(handle != handle2);
211
212 dlclose(handle2);
213 dlclose(handle);
214}
215
216TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
217 // Check if soname lookup still returns already loaded library
218 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
219 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
220 ASSERT_DL_NOTNULL(handle);
221
222 android_dlextinfo extinfo;
223 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
224
225 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
226 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
227
228 ASSERT_DL_NOTNULL(handle2);
229 ASSERT_TRUE(handle == handle2);
230
231 dlclose(handle2);
232 dlclose(handle);
233}
234
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700235TEST(dlfcn, dlopen_from_zip_absolute_path) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700236 std::string lib_path;
237 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700238
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700239 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700240 ASSERT_TRUE(handle != nullptr) << dlerror();
241
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700242 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
243 ASSERT_DL_NOTNULL(taxicab_number);
244 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700245
246 dlclose(handle);
247}
248
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700249TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700250 std::string data_path;
251 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
252 const std::string lib_path = data_path + LIBZIPPATH_WITH_RUNPATH;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700253
254 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
255
256 ASSERT_TRUE(handle != nullptr) << dlerror();
257
258 typedef void *(* dlopen_b_fn)();
259 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
260 ASSERT_TRUE(fn != nullptr) << dlerror();
261
262 void *p = fn();
263 ASSERT_TRUE(p != nullptr) << dlerror();
264
265 dlclose(p);
266 dlclose(handle);
267}
268
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700269TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700270 std::string data_path;
271 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
272 const std::string lib_path = data_path + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700273
274 typedef void (*fn_t)(const char*);
275 fn_t android_update_LD_LIBRARY_PATH =
276 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
277
278 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
279
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700280 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700281 ASSERT_TRUE(handle == nullptr);
282
283 android_update_LD_LIBRARY_PATH(lib_path.c_str());
284
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700285 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700286 ASSERT_TRUE(handle != nullptr) << dlerror();
287
288 int (*fn)(void);
289 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
290 ASSERT_TRUE(fn != nullptr);
291 EXPECT_EQ(4, fn());
292
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800293 uint32_t* taxicab_number =
294 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700295 ASSERT_DL_NOTNULL(taxicab_number);
296 EXPECT_EQ(1729U, *taxicab_number);
297
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700298 dlclose(handle);
299}
300
301
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000302TEST_F(DlExtTest, Reserved) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800303 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000304 ASSERT_TRUE(start != MAP_FAILED);
305 android_dlextinfo extinfo;
306 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
307 extinfo.reserved_addr = start;
308 extinfo.reserved_size = LIBSIZE;
309 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
310 ASSERT_DL_NOTNULL(handle_);
311 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
312 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700313 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000314 EXPECT_LT(reinterpret_cast<void*>(f),
315 reinterpret_cast<char*>(start) + LIBSIZE);
316 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800317
318 // Check that after dlclose reserved address space is unmapped (and can be reused)
319 dlclose(handle_);
320 handle_ = nullptr;
321
322 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
323 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000324}
325
326TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800327 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000328 ASSERT_TRUE(start != MAP_FAILED);
329 android_dlextinfo extinfo;
330 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
331 extinfo.reserved_addr = start;
332 extinfo.reserved_size = PAGE_SIZE;
333 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700334 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000335}
336
337TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800338 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000339 ASSERT_TRUE(start != MAP_FAILED);
340 android_dlextinfo extinfo;
341 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
342 extinfo.reserved_addr = start;
343 extinfo.reserved_size = LIBSIZE;
344 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
345 ASSERT_DL_NOTNULL(handle_);
346 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
347 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700348 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000349 EXPECT_LT(reinterpret_cast<void*>(f),
350 reinterpret_cast<char*>(start) + LIBSIZE);
351 EXPECT_EQ(4, f());
352}
353
354TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800355 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000356 ASSERT_TRUE(start != MAP_FAILED);
357 android_dlextinfo extinfo;
358 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
359 extinfo.reserved_addr = start;
360 extinfo.reserved_size = PAGE_SIZE;
361 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
362 ASSERT_DL_NOTNULL(handle_);
363 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
364 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700365 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
366 (reinterpret_cast<void*>(f) >=
367 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000368 EXPECT_EQ(4, f());
369}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000370
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700371TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800372 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700373 ASSERT_TRUE(start != MAP_FAILED);
374 munmap(start, LIBSIZE);
375
376 android_dlextinfo extinfo;
377 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
378 extinfo.reserved_addr = start;
379
380 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
381 ASSERT_DL_NOTNULL(handle_);
382 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
383 ASSERT_DL_NOTNULL(f);
384 EXPECT_GE(reinterpret_cast<void*>(f), start);
385 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
386
387 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800388 dlclose(handle_);
389 handle_ = nullptr;
390
391 // Check that dlclose unmapped the file
392 void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
393 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700394}
395
396TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
397 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
398 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
399 ASSERT_TRUE(start != MAP_FAILED);
400 munmap(start, LIBSIZE + PAGE_SIZE);
401 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
402 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
403 ASSERT_TRUE(new_addr != MAP_FAILED);
404
405 android_dlextinfo extinfo;
406 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
407 extinfo.reserved_addr = start;
408
409 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
410 ASSERT_TRUE(handle_ == nullptr);
411}
412
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100413class DlExtRelroSharingTest : public DlExtTest {
414protected:
415 virtual void SetUp() {
416 DlExtTest::SetUp();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800417 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100418 ASSERT_TRUE(start != MAP_FAILED);
419 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
420 extinfo_.reserved_addr = start;
421 extinfo_.reserved_size = LIBSIZE;
422 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000423 }
424
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100425 virtual void TearDown() {
426 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100427 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000428
Yabin Cui294d1e22014-12-07 20:43:37 -0800429 void CreateRelroFile(const char* lib, const char* relro_file) {
430 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100431 ASSERT_NOERROR(relro_fd);
432
433 pid_t pid = fork();
434 if (pid == 0) {
435 // child process
436 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
437 extinfo_.relro_fd = relro_fd;
438 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700439 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100440 fprintf(stderr, "in child: %s\n", dlerror());
441 exit(1);
442 }
443 exit(0);
444 }
445
446 // continuing in parent
447 ASSERT_NOERROR(close(relro_fd));
448 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800449 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100450
451 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800452 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100453 ASSERT_NOERROR(relro_fd);
454 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
455 extinfo_.relro_fd = relro_fd;
456 }
457
458 void TryUsingRelro(const char* lib) {
459 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
460 ASSERT_DL_NOTNULL(handle_);
461 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
462 ASSERT_DL_NOTNULL(f);
463 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700464
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800465 uint32_t* taxicab_number =
466 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700467 ASSERT_DL_NOTNULL(taxicab_number);
468 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100469 }
470
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100471 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
472
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100473 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100474};
475
476TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800477 TemporaryFile tf; // Use tf to get an unique filename.
478 ASSERT_NOERROR(close(tf.fd));
479
480 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100481 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800482
483 // Use destructor of tf to close and unlink the file.
484 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100485}
486
487TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800488 TemporaryFile tf; // // Use tf to get an unique filename.
489 ASSERT_NOERROR(close(tf.fd));
490
491 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100492 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800493
494 // Use destructor of tf to close and unlink the file.
495 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100496}
497
498TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100499 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000500}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100501
502TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700503 if (geteuid() != 0) {
504 GTEST_LOG_(INFO) << "This test must be run as root.\n";
505 return;
506 }
507
Yabin Cui294d1e22014-12-07 20:43:37 -0800508 TemporaryFile tf; // Use tf to get an unique filename.
509 ASSERT_NOERROR(close(tf.fd));
510
511 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
512
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100513 int pipefd[2];
514 ASSERT_NOERROR(pipe(pipefd));
515
516 size_t without_sharing, with_sharing;
517 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
518 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
519
520 // We expect the sharing to save at least 10% of the total PSS. In practice
521 // it saves 40%+ for this test.
522 size_t expected_size = without_sharing - (without_sharing/10);
523 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800524
525 // Use destructor of tf to close and unlink the file.
526 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100527}
528
529void getPss(pid_t pid, size_t* pss_out) {
530 pm_kernel_t* kernel;
531 ASSERT_EQ(0, pm_kernel_create(&kernel));
532
533 pm_process_t* process;
534 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
535
536 pm_map_t** maps;
537 size_t num_maps;
538 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
539
540 size_t total_pss = 0;
541 for (size_t i = 0; i < num_maps; i++) {
542 pm_memusage_t usage;
543 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
544 total_pss += usage.pss;
545 }
546 *pss_out = total_pss;
547
548 free(maps);
549 pm_process_destroy(process);
550 pm_kernel_destroy(kernel);
551}
552
553void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
554 size_t* pss_out) {
555 const int CHILDREN = 20;
556
557 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800558 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100559 int childpipe[CHILDREN];
560 for (int i=0; i<CHILDREN; ++i) {
561 char read_buf;
562 int child_done_pipe[2], parent_done_pipe[2];
563 ASSERT_NOERROR(pipe(child_done_pipe));
564 ASSERT_NOERROR(pipe(parent_done_pipe));
565
566 pid_t child = fork();
567 if (child == 0) {
568 // close the 'wrong' ends of the pipes in the child
569 close(child_done_pipe[0]);
570 close(parent_done_pipe[1]);
571
572 // open the library
573 void* handle;
574 if (share_relro) {
575 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
576 } else {
577 handle = dlopen(lib, RTLD_NOW);
578 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700579 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100580 fprintf(stderr, "in child: %s\n", dlerror());
581 exit(1);
582 }
583
584 // close write end of child_done_pipe to signal the parent that we're done.
585 close(child_done_pipe[1]);
586
587 // wait for the parent to close parent_done_pipe, then exit
588 read(parent_done_pipe[0], &read_buf, 1);
589 exit(0);
590 }
591
592 ASSERT_NOERROR(child);
593
594 // close the 'wrong' ends of the pipes in the parent
595 close(child_done_pipe[1]);
596 close(parent_done_pipe[0]);
597
598 // wait for the child to be done
599 read(child_done_pipe[0], &read_buf, 1);
600 close(child_done_pipe[0]);
601
602 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800603 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100604 childpipe[i] = parent_done_pipe[1];
605 }
606
607 // Sum the PSS of all the children
608 size_t total_pss = 0;
609 for (int i=0; i<CHILDREN; ++i) {
610 size_t child_pss;
Elliott Hughes33697a02016-01-26 13:04:57 -0800611 ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100612 total_pss += child_pss;
613 }
614 *pss_out = total_pss;
615
616 // Close pipes and wait for children to exit
617 for (int i=0; i<CHILDREN; ++i) {
618 ASSERT_NOERROR(close(childpipe[i]));
619 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800620 for (int i = 0; i < CHILDREN; ++i) {
621 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100622 }
623}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700624
625// Testing namespaces
626static const char* g_public_lib = "libnstest_public.so";
627
628TEST(dlext, ns_smoke) {
629 static const char* root_lib = "libnstest_root.so";
630 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
631
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800632 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
633 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dimitry Ivanov3a6c6b32016-07-13 16:28:20 -0700634 "a library with soname \"libnstest_public.so\" was not found in the "
635 "default namespace",
636 dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700637
Dimitry Ivanov54807612016-04-21 14:57:38 -0700638 ASSERT_FALSE(android_init_namespaces("", nullptr));
639 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
640 "the list of public libraries is empty.", dlerror());
641
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700642 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
643
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800644 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
645 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700646 ASSERT_TRUE(handle_public != nullptr) << dlerror();
647
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800648 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700649
650 // Check that libraries added to public namespace are NODELETE
651 dlclose(handle_public);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800652 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
653 RTLD_NOW | RTLD_NOLOAD);
654
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700655 ASSERT_TRUE(handle_public != nullptr) << dlerror();
656
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800657 android_namespace_t* ns1 =
658 android_create_namespace("private", nullptr,
659 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700660 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700661 ASSERT_TRUE(ns1 != nullptr) << dlerror();
662
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800663 android_namespace_t* ns2 =
664 android_create_namespace("private_isolated", nullptr,
665 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700666 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700667 ASSERT_TRUE(ns2 != nullptr) << dlerror();
668
669 // This should not have affect search path for default namespace:
670 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
671 void* handle = dlopen(g_public_lib, RTLD_NOW);
672 ASSERT_TRUE(handle != nullptr) << dlerror();
673 dlclose(handle);
674
675 android_dlextinfo extinfo;
676 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
677 extinfo.library_namespace = ns1;
678
679 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
680 ASSERT_TRUE(handle1 != nullptr) << dlerror();
681
682 extinfo.library_namespace = ns2;
683 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
684 ASSERT_TRUE(handle2 != nullptr) << dlerror();
685
686 ASSERT_TRUE(handle1 != handle2);
687
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800688 // dlopen for a public library using an absolute path should work for isolated namespaces
689 extinfo.library_namespace = ns2;
690 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
691 ASSERT_TRUE(handle != nullptr) << dlerror();
692 ASSERT_TRUE(handle == handle_public);
693
694 dlclose(handle);
695
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700696 typedef const char* (*fn_t)();
697
698 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
699 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
700 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
701 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
702
703 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
704 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
705
706 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
707
708 fn_t ns_get_private_extern_string1 =
709 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
710 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
711 fn_t ns_get_private_extern_string2 =
712 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
713 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
714
715 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
716 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
717
718 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
719
720 fn_t ns_get_public_extern_string1 =
721 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
722 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
723 fn_t ns_get_public_extern_string2 =
724 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
725 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
726
727 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
728 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
729
730 // and now check that dlopen() does the right thing in terms of preserving namespace
731 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
732 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
733 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
734 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
735
736 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
737 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
738
739 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
740
741 dlclose(handle1);
742
743 // Check if handle2 is still alive (and well)
744 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
745 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
746 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
747 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
748
749 dlclose(handle2);
750}
751
752TEST(dlext, ns_isolated) {
753 static const char* root_lib = "libnstest_root_not_isolated.so";
754 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
755
756 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800757 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
758 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700759 ASSERT_TRUE(handle_public != nullptr) << dlerror();
760
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800761 android_set_application_target_sdk_version(42U); // something > 23
762
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800763 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700764
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800765 android_namespace_t* ns_not_isolated =
766 android_create_namespace("private", nullptr,
767 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700768 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700769 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
770
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800771 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700772 android_create_namespace("private_isolated1",
773 nullptr,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800774 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700775 ANDROID_NAMESPACE_TYPE_ISOLATED,
776 nullptr,
777 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700778 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
779
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800780 android_namespace_t* ns_isolated2 =
781 android_create_namespace("private_isolated2",
782 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700783 nullptr,
784 ANDROID_NAMESPACE_TYPE_ISOLATED,
785 lib_path.c_str(),
786 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700787 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
788
789 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
790 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
791
792 std::string lib_private_external_path =
793 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
794
795 // Load lib_private_external_path to default namespace
796 // (it should remain invisible for the isolated namespaces after this)
797 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
798 ASSERT_TRUE(handle != nullptr) << dlerror();
799
800 android_dlextinfo extinfo;
801 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
802 extinfo.library_namespace = ns_not_isolated;
803
804 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
805 ASSERT_TRUE(handle1 != nullptr) << dlerror();
806
807 extinfo.library_namespace = ns_isolated;
808
809 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
810 ASSERT_TRUE(handle2 == nullptr);
811 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
812
813 // Check dlopen by absolute path
814 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
815 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800816 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700817 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800818 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700819
820 extinfo.library_namespace = ns_isolated2;
821
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800822 // this should work because isolation_path for private_isolated2 includes lib_path
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700823 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800824 ASSERT_TRUE(handle2 != nullptr) << dlerror();
825 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700826
827 // Check dlopen by absolute path
828 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), 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 typedef const char* (*fn_t)();
833 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
834 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
835
836 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
837
838 fn_t ns_get_private_extern_string =
839 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
840 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
841
842 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
843
844 fn_t ns_get_public_extern_string =
845 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
846 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
847
848 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
849
850 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
851 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
852
853 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
854
855 dlclose(handle1);
856}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800857
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800858TEST(dlext, ns_shared) {
859 static const char* root_lib = "libnstest_root_not_isolated.so";
860 static const char* root_lib_isolated = "libnstest_root.so";
861 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
862
863 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
864 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
865 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
866 ASSERT_TRUE(handle_public != nullptr) << dlerror();
867
868 android_set_application_target_sdk_version(42U); // something > 23
869
870 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
871
872 // preload this library to the default namespace to check if it
873 // is shared later on.
874 void* handle_dlopened =
875 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
876 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
877
878 android_namespace_t* ns_not_isolated =
879 android_create_namespace("private", nullptr,
880 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700881 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800882 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
883
884 android_namespace_t* ns_isolated_shared =
885 android_create_namespace("private_isolated_shared", nullptr,
886 (lib_path + "/private_namespace_libs").c_str(),
887 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700888 nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800889 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
890
891 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
892 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
893
894 std::string lib_private_external_path =
895 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
896
897 // Load lib_private_external_path to default namespace
898 // (it should remain invisible for the isolated namespaces after this)
899 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
900 ASSERT_TRUE(handle != nullptr) << dlerror();
901
902 android_dlextinfo extinfo;
903 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
904 extinfo.library_namespace = ns_not_isolated;
905
906 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
907 ASSERT_TRUE(handle1 != nullptr) << dlerror();
908
909 extinfo.library_namespace = ns_isolated_shared;
910
911 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
912 ASSERT_TRUE(handle2 == nullptr);
913 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
914
915 // Check dlopen by absolute path
916 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
917 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800918 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700919 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800920 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800921
922 // load libnstest_root.so to shared namespace in order to check that everything is different
923 // except shared libnstest_dlopened.so
924
925 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
926
927 typedef const char* (*fn_t)();
928 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
929 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
930 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
931 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
932
933 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
934 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
935 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
936
937 fn_t ns_get_private_extern_string =
938 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
939 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
940 fn_t ns_get_private_extern_string_shared =
941 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
942 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
943
944 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
945 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
946 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
947
948 fn_t ns_get_public_extern_string =
949 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
950 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
951 fn_t ns_get_public_extern_string_shared =
952 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
953 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
954
955 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
956 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
957 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
958
959 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
960 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
961 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
962 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
963 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
964 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
965
966 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
967 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
968 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
969 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
970 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
971
972 dlclose(handle1);
973 dlclose(handle2);
974}
975
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700976TEST(dlext, ns_shared_dlclose) {
977 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
978
979 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
980
981 android_set_application_target_sdk_version(42U); // something > 23
982
983 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
984
985 // preload this library to the default namespace to check if it
986 // is shared later on.
987 void* handle_dlopened =
988 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
989 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
990
991 android_namespace_t* ns_isolated_shared =
992 android_create_namespace("private_isolated_shared", nullptr,
993 (lib_path + "/private_namespace_libs").c_str(),
994 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700995 nullptr, nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700996 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
997
998 // Check if "libnstest_dlopened.so" is loaded (and the same)
999 android_dlextinfo extinfo;
1000 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1001 extinfo.library_namespace = ns_isolated_shared;
1002
1003 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1004 ASSERT_TRUE(handle != nullptr) << dlerror();
1005 ASSERT_TRUE(handle == handle_dlopened);
1006 dlclose(handle);
1007 dlclose(handle_dlopened);
1008
1009 // And now check that the library cannot be found by soname (and is no longer loaded)
1010 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1011 ASSERT_TRUE(handle == nullptr)
1012 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1013
1014 handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1015 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1016 ASSERT_TRUE(handle == nullptr)
1017 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1018
1019 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1020 ASSERT_TRUE(handle == nullptr)
1021 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1022
1023 handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1024 RTLD_NOW | RTLD_NOLOAD);
1025 ASSERT_TRUE(handle == nullptr)
1026 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1027
1028 // Now lets see if the soinfo area gets reused in the wrong way:
1029 // load a library to default namespace.
1030 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1031 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1032 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1033
1034 // try to find it in shared namespace
1035 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1036 ASSERT_TRUE(handle == nullptr)
1037 << "Error: " << g_public_lib << " is accessible in shared namespace";
1038}
1039
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001040TEST(dlext, ns_isolated_rtld_global) {
1041 static const char* root_lib = "libnstest_root.so";
1042 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
1043
1044 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
1045
1046 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
1047
1048 const std::string lib_public_path = lib_path + "/public_namespace_libs";
1049
1050 android_namespace_t* ns1 =
1051 android_create_namespace("isolated1",
1052 nullptr,
1053 (lib_path + "/private_namespace_libs").c_str(),
1054 ANDROID_NAMESPACE_TYPE_ISOLATED,
1055 lib_public_path.c_str(),
1056 nullptr);
1057 ASSERT_TRUE(ns1 != nullptr) << dlerror();
1058
1059 android_namespace_t* ns2 =
1060 android_create_namespace("isolated2",
1061 nullptr,
1062 (lib_path + "/private_namespace_libs").c_str(),
1063 ANDROID_NAMESPACE_TYPE_ISOLATED,
1064 lib_public_path.c_str(),
1065 nullptr);
1066 ASSERT_TRUE(ns2 != nullptr) << dlerror();
1067
1068 android_dlextinfo extinfo;
1069 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1070 extinfo.library_namespace = ns1;
1071
1072 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1073 RTLD_GLOBAL,
1074 &extinfo);
1075
1076 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1077
1078 android_namespace_t* ns1_child =
1079 android_create_namespace("isolated1_child",
1080 nullptr,
1081 (lib_path + "/private_namespace_libs").c_str(),
1082 ANDROID_NAMESPACE_TYPE_ISOLATED,
1083 nullptr,
1084 ns1);
1085
1086 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1087 // attempt to use ns2 should result in dlerror()
1088
1089 // Check ns1_child first.
1090 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1091 extinfo.library_namespace = ns1_child;
1092
1093 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1094 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1095
1096 // now ns1
1097 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1098 extinfo.library_namespace = ns1;
1099
1100 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1101 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1102
1103 // and ns2 should fail
1104 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1105 extinfo.library_namespace = ns2;
1106
1107 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1108 ASSERT_TRUE(handle1 == nullptr);
1109 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1110}
1111
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001112TEST(dlext, ns_anonymous) {
1113 static const char* root_lib = "libnstest_root.so";
1114 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
1115
Dimitry Ivanova36e59b2016-09-01 11:37:39 -07001116 std::string data_path;
1117 ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
1118 const std::string lib_path = data_path + NATIVE_TESTS_PATH;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001119
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001120 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1121 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1122
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001123 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1124
1125 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
1126 << dlerror();
1127
1128 android_namespace_t* ns = android_create_namespace(
1129 "private", nullptr,
1130 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001131 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001132
1133 ASSERT_TRUE(ns != nullptr) << dlerror();
1134
1135 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
1136
1137 android_dlextinfo extinfo;
1138 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1139 extinfo.library_namespace = ns;
1140
1141 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1142 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1143 ASSERT_TRUE(handle != nullptr) << dlerror();
1144
1145 uintptr_t ns_get_dlopened_string_addr =
1146 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1147 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1148 typedef const char* (*fn_t)();
1149 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1150
1151 std::vector<map_record> maps;
1152 Maps::parse_maps(&maps);
1153
1154 uintptr_t addr_start = 0;
1155 uintptr_t addr_end = 0;
1156 std::vector<map_record> maps_to_copy;
1157
1158 for (const auto& rec : maps) {
1159 if (rec.pathname == private_library_absolute_path) {
1160 if (addr_start == 0) {
1161 addr_start = rec.addr_start;
1162 }
1163 addr_end = rec.addr_end;
1164
1165 maps_to_copy.push_back(rec);
1166 }
1167 }
1168
1169 // some sanity checks..
1170 ASSERT_TRUE(addr_start > 0);
1171 ASSERT_TRUE(addr_end > 0);
1172 ASSERT_EQ(3U, maps_to_copy.size());
1173 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1174 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1175
1176 // copy
1177 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1178 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1179 -1, 0));
1180 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1181
1182 for (const auto& rec : maps_to_copy) {
1183 uintptr_t offset = rec.addr_start - addr_start;
1184 size_t size = rec.addr_end - rec.addr_start;
1185 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1186 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1187 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1188 ASSERT_TRUE(map != MAP_FAILED);
1189 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1190 mprotect(map, size, rec.perms);
1191 }
1192
1193 // call the function copy
1194 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1195 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1196 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1197 ns_get_dlopened_string_anon());
1198
1199 // They should belong to different namespaces (private and anonymous)
1200 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1201 ns_get_dlopened_string_private());
1202
1203 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1204}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001205
1206TEST(dlext, dlopen_handle_value_platform) {
1207 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1208 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1209 << "dlopen should return odd value for the handle";
1210 dlclose(handle);
1211}
1212
1213TEST(dlext, dlopen_handle_value_app_compat) {
1214 android_set_application_target_sdk_version(23);
1215 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1216 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1217 << "dlopen should return valid pointer";
1218 dlclose(handle);
1219}
1220