blob: c3230e75e82db8d1354700e73f52b1b245a942e5 [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__)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070058#define NATIVE_TESTS_PATH "/nativetest64"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070059#else
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070060#define NATIVE_TESTS_PATH "/nativetest"
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 Ivanov52393a52015-03-18 22:50:01 -0700153 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700154 // lib_path is relative when $ANDROID_DATA is relative
155 char lib_realpath_buf[PATH_MAX];
156 ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
157 const std::string lib_realpath = std::string(lib_realpath_buf);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700158
159 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700160 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700161 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700162 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700163
164 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
165 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700166 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
167
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800168 // Test an address above 2^44, for http://b/18178121 .
169 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700170 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800172 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
173
174 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
175 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
176 ASSERT_TRUE(handle_ == nullptr);
177 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
178
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700179 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700180 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800181 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700182 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700183
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800184 // Check if dlsym works after unsuccessful dlopen().
185 // Supply non-exiting one to make linker visit every soinfo.
186 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
187 ASSERT_TRUE(sym == nullptr);
188
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700189 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700190}
191
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800192TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700193 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700194 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Christopher Ferrisc0ffcec2016-01-19 20:32:37 -0800195 // This offset will not be used, so it doesn't matter.
196 extinfo.library_fd_offset = 0;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700197
198 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
199 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700200 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 -0700201}
202
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700203TEST(dlext, android_dlopen_ext_force_load_smoke) {
204 // 1. Open actual file
205 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
206 ASSERT_DL_NOTNULL(handle);
207 // 2. Open link with force_load flag set
208 android_dlextinfo extinfo;
209 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
210 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
211 ASSERT_DL_NOTNULL(handle2);
212 ASSERT_TRUE(handle != handle2);
213
214 dlclose(handle2);
215 dlclose(handle);
216}
217
218TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
219 // Check if soname lookup still returns already loaded library
220 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
221 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
222 ASSERT_DL_NOTNULL(handle);
223
224 android_dlextinfo extinfo;
225 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
226
227 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
228 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
229
230 ASSERT_DL_NOTNULL(handle2);
231 ASSERT_TRUE(handle == handle2);
232
233 dlclose(handle2);
234 dlclose(handle);
235}
236
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700237TEST(dlfcn, dlopen_from_zip_absolute_path) {
238 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
239
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700240 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700241 ASSERT_TRUE(handle != nullptr) << dlerror();
242
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700243 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
244 ASSERT_DL_NOTNULL(taxicab_number);
245 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700246
247 dlclose(handle);
248}
249
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700250TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
251 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH;
252
253 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
254
255 ASSERT_TRUE(handle != nullptr) << dlerror();
256
257 typedef void *(* dlopen_b_fn)();
258 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
259 ASSERT_TRUE(fn != nullptr) << dlerror();
260
261 void *p = fn();
262 ASSERT_TRUE(p != nullptr) << dlerror();
263
264 dlclose(p);
265 dlclose(handle);
266}
267
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700268TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700269 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700270
271 typedef void (*fn_t)(const char*);
272 fn_t android_update_LD_LIBRARY_PATH =
273 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
274
275 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
276
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700277 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700278 ASSERT_TRUE(handle == nullptr);
279
280 android_update_LD_LIBRARY_PATH(lib_path.c_str());
281
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700282 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700283 ASSERT_TRUE(handle != nullptr) << dlerror();
284
285 int (*fn)(void);
286 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
287 ASSERT_TRUE(fn != nullptr);
288 EXPECT_EQ(4, fn());
289
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800290 uint32_t* taxicab_number =
291 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700292 ASSERT_DL_NOTNULL(taxicab_number);
293 EXPECT_EQ(1729U, *taxicab_number);
294
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700295 dlclose(handle);
296}
297
298
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000299TEST_F(DlExtTest, Reserved) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800300 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000301 ASSERT_TRUE(start != MAP_FAILED);
302 android_dlextinfo extinfo;
303 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
304 extinfo.reserved_addr = start;
305 extinfo.reserved_size = LIBSIZE;
306 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
307 ASSERT_DL_NOTNULL(handle_);
308 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
309 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700310 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000311 EXPECT_LT(reinterpret_cast<void*>(f),
312 reinterpret_cast<char*>(start) + LIBSIZE);
313 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800314
315 // Check that after dlclose reserved address space is unmapped (and can be reused)
316 dlclose(handle_);
317 handle_ = nullptr;
318
319 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
320 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000321}
322
323TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800324 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000325 ASSERT_TRUE(start != MAP_FAILED);
326 android_dlextinfo extinfo;
327 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
328 extinfo.reserved_addr = start;
329 extinfo.reserved_size = PAGE_SIZE;
330 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700331 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000332}
333
334TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800335 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000336 ASSERT_TRUE(start != MAP_FAILED);
337 android_dlextinfo extinfo;
338 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
339 extinfo.reserved_addr = start;
340 extinfo.reserved_size = LIBSIZE;
341 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
342 ASSERT_DL_NOTNULL(handle_);
343 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
344 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700345 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000346 EXPECT_LT(reinterpret_cast<void*>(f),
347 reinterpret_cast<char*>(start) + LIBSIZE);
348 EXPECT_EQ(4, f());
349}
350
351TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800352 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000353 ASSERT_TRUE(start != MAP_FAILED);
354 android_dlextinfo extinfo;
355 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
356 extinfo.reserved_addr = start;
357 extinfo.reserved_size = PAGE_SIZE;
358 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
359 ASSERT_DL_NOTNULL(handle_);
360 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
361 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700362 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
363 (reinterpret_cast<void*>(f) >=
364 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000365 EXPECT_EQ(4, f());
366}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000367
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700368TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800369 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700370 ASSERT_TRUE(start != MAP_FAILED);
371 munmap(start, LIBSIZE);
372
373 android_dlextinfo extinfo;
374 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
375 extinfo.reserved_addr = start;
376
377 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
378 ASSERT_DL_NOTNULL(handle_);
379 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
380 ASSERT_DL_NOTNULL(f);
381 EXPECT_GE(reinterpret_cast<void*>(f), start);
382 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
383
384 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800385 dlclose(handle_);
386 handle_ = nullptr;
387
388 // Check that dlclose unmapped the file
389 void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
390 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700391}
392
393TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
394 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
395 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
396 ASSERT_TRUE(start != MAP_FAILED);
397 munmap(start, LIBSIZE + PAGE_SIZE);
398 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
399 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
400 ASSERT_TRUE(new_addr != MAP_FAILED);
401
402 android_dlextinfo extinfo;
403 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
404 extinfo.reserved_addr = start;
405
406 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
407 ASSERT_TRUE(handle_ == nullptr);
408}
409
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410class DlExtRelroSharingTest : public DlExtTest {
411protected:
412 virtual void SetUp() {
413 DlExtTest::SetUp();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800414 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100415 ASSERT_TRUE(start != MAP_FAILED);
416 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
417 extinfo_.reserved_addr = start;
418 extinfo_.reserved_size = LIBSIZE;
419 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000420 }
421
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100422 virtual void TearDown() {
423 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100424 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000425
Yabin Cui294d1e22014-12-07 20:43:37 -0800426 void CreateRelroFile(const char* lib, const char* relro_file) {
427 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100428 ASSERT_NOERROR(relro_fd);
429
430 pid_t pid = fork();
431 if (pid == 0) {
432 // child process
433 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
434 extinfo_.relro_fd = relro_fd;
435 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700436 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100437 fprintf(stderr, "in child: %s\n", dlerror());
438 exit(1);
439 }
440 exit(0);
441 }
442
443 // continuing in parent
444 ASSERT_NOERROR(close(relro_fd));
445 ASSERT_NOERROR(pid);
Elliott Hughes33697a02016-01-26 13:04:57 -0800446 AssertChildExited(pid, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100447
448 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800449 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100450 ASSERT_NOERROR(relro_fd);
451 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
452 extinfo_.relro_fd = relro_fd;
453 }
454
455 void TryUsingRelro(const char* lib) {
456 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
457 ASSERT_DL_NOTNULL(handle_);
458 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
459 ASSERT_DL_NOTNULL(f);
460 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700461
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800462 uint32_t* taxicab_number =
463 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700464 ASSERT_DL_NOTNULL(taxicab_number);
465 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100466 }
467
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100468 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
469
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100470 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100471};
472
473TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800474 TemporaryFile tf; // Use tf to get an unique filename.
475 ASSERT_NOERROR(close(tf.fd));
476
477 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100478 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800479
480 // Use destructor of tf to close and unlink the file.
481 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100482}
483
484TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800485 TemporaryFile tf; // // Use tf to get an unique filename.
486 ASSERT_NOERROR(close(tf.fd));
487
488 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100489 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800490
491 // Use destructor of tf to close and unlink the file.
492 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100493}
494
495TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100496 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000497}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100498
499TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700500 if (geteuid() != 0) {
501 GTEST_LOG_(INFO) << "This test must be run as root.\n";
502 return;
503 }
504
Yabin Cui294d1e22014-12-07 20:43:37 -0800505 TemporaryFile tf; // Use tf to get an unique filename.
506 ASSERT_NOERROR(close(tf.fd));
507
508 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
509
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100510 int pipefd[2];
511 ASSERT_NOERROR(pipe(pipefd));
512
513 size_t without_sharing, with_sharing;
514 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
515 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
516
517 // We expect the sharing to save at least 10% of the total PSS. In practice
518 // it saves 40%+ for this test.
519 size_t expected_size = without_sharing - (without_sharing/10);
520 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800521
522 // Use destructor of tf to close and unlink the file.
523 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100524}
525
526void getPss(pid_t pid, size_t* pss_out) {
527 pm_kernel_t* kernel;
528 ASSERT_EQ(0, pm_kernel_create(&kernel));
529
530 pm_process_t* process;
531 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
532
533 pm_map_t** maps;
534 size_t num_maps;
535 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
536
537 size_t total_pss = 0;
538 for (size_t i = 0; i < num_maps; i++) {
539 pm_memusage_t usage;
540 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
541 total_pss += usage.pss;
542 }
543 *pss_out = total_pss;
544
545 free(maps);
546 pm_process_destroy(process);
547 pm_kernel_destroy(kernel);
548}
549
550void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
551 size_t* pss_out) {
552 const int CHILDREN = 20;
553
554 // Create children
Elliott Hughes33697a02016-01-26 13:04:57 -0800555 pid_t child_pids[CHILDREN];
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100556 int childpipe[CHILDREN];
557 for (int i=0; i<CHILDREN; ++i) {
558 char read_buf;
559 int child_done_pipe[2], parent_done_pipe[2];
560 ASSERT_NOERROR(pipe(child_done_pipe));
561 ASSERT_NOERROR(pipe(parent_done_pipe));
562
563 pid_t child = fork();
564 if (child == 0) {
565 // close the 'wrong' ends of the pipes in the child
566 close(child_done_pipe[0]);
567 close(parent_done_pipe[1]);
568
569 // open the library
570 void* handle;
571 if (share_relro) {
572 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
573 } else {
574 handle = dlopen(lib, RTLD_NOW);
575 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700576 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100577 fprintf(stderr, "in child: %s\n", dlerror());
578 exit(1);
579 }
580
581 // close write end of child_done_pipe to signal the parent that we're done.
582 close(child_done_pipe[1]);
583
584 // wait for the parent to close parent_done_pipe, then exit
585 read(parent_done_pipe[0], &read_buf, 1);
586 exit(0);
587 }
588
589 ASSERT_NOERROR(child);
590
591 // close the 'wrong' ends of the pipes in the parent
592 close(child_done_pipe[1]);
593 close(parent_done_pipe[0]);
594
595 // wait for the child to be done
596 read(child_done_pipe[0], &read_buf, 1);
597 close(child_done_pipe[0]);
598
599 // save the child's pid and the parent_done_pipe
Elliott Hughes33697a02016-01-26 13:04:57 -0800600 child_pids[i] = child;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100601 childpipe[i] = parent_done_pipe[1];
602 }
603
604 // Sum the PSS of all the children
605 size_t total_pss = 0;
606 for (int i=0; i<CHILDREN; ++i) {
607 size_t child_pss;
Elliott Hughes33697a02016-01-26 13:04:57 -0800608 ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100609 total_pss += child_pss;
610 }
611 *pss_out = total_pss;
612
613 // Close pipes and wait for children to exit
614 for (int i=0; i<CHILDREN; ++i) {
615 ASSERT_NOERROR(close(childpipe[i]));
616 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800617 for (int i = 0; i < CHILDREN; ++i) {
618 AssertChildExited(child_pids[i], 0);
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100619 }
620}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700621
622// Testing namespaces
623static const char* g_public_lib = "libnstest_public.so";
624
625TEST(dlext, ns_smoke) {
626 static const char* root_lib = "libnstest_root.so";
627 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
628
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800629 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
630 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dimitry Ivanov3a6c6b32016-07-13 16:28:20 -0700631 "a library with soname \"libnstest_public.so\" was not found in the "
632 "default namespace",
633 dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700634
Dimitry Ivanov54807612016-04-21 14:57:38 -0700635 ASSERT_FALSE(android_init_namespaces("", nullptr));
636 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
637 "the list of public libraries is empty.", dlerror());
638
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700639 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
640
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800641 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
642 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700643 ASSERT_TRUE(handle_public != nullptr) << dlerror();
644
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800645 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700646
647 // Check that libraries added to public namespace are NODELETE
648 dlclose(handle_public);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800649 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
650 RTLD_NOW | RTLD_NOLOAD);
651
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700652 ASSERT_TRUE(handle_public != nullptr) << dlerror();
653
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800654 android_namespace_t* ns1 =
655 android_create_namespace("private", nullptr,
656 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700657 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700658 ASSERT_TRUE(ns1 != nullptr) << dlerror();
659
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800660 android_namespace_t* ns2 =
661 android_create_namespace("private_isolated", nullptr,
662 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700663 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700664 ASSERT_TRUE(ns2 != nullptr) << dlerror();
665
666 // This should not have affect search path for default namespace:
667 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
668 void* handle = dlopen(g_public_lib, RTLD_NOW);
669 ASSERT_TRUE(handle != nullptr) << dlerror();
670 dlclose(handle);
671
672 android_dlextinfo extinfo;
673 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
674 extinfo.library_namespace = ns1;
675
676 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
677 ASSERT_TRUE(handle1 != nullptr) << dlerror();
678
679 extinfo.library_namespace = ns2;
680 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
681 ASSERT_TRUE(handle2 != nullptr) << dlerror();
682
683 ASSERT_TRUE(handle1 != handle2);
684
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800685 // dlopen for a public library using an absolute path should work for isolated namespaces
686 extinfo.library_namespace = ns2;
687 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
688 ASSERT_TRUE(handle != nullptr) << dlerror();
689 ASSERT_TRUE(handle == handle_public);
690
691 dlclose(handle);
692
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700693 typedef const char* (*fn_t)();
694
695 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
696 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
697 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
698 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
699
700 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
701 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
702
703 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
704
705 fn_t ns_get_private_extern_string1 =
706 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
707 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
708 fn_t ns_get_private_extern_string2 =
709 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
710 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
711
712 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
713 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
714
715 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
716
717 fn_t ns_get_public_extern_string1 =
718 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
719 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
720 fn_t ns_get_public_extern_string2 =
721 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
722 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
723
724 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
725 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
726
727 // and now check that dlopen() does the right thing in terms of preserving namespace
728 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
729 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
730 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
731 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
732
733 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
734 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
735
736 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
737
738 dlclose(handle1);
739
740 // Check if handle2 is still alive (and well)
741 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
742 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
743 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
744 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
745
746 dlclose(handle2);
747}
748
749TEST(dlext, ns_isolated) {
750 static const char* root_lib = "libnstest_root_not_isolated.so";
751 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
752
753 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800754 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
755 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700756 ASSERT_TRUE(handle_public != nullptr) << dlerror();
757
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800758 android_set_application_target_sdk_version(42U); // something > 23
759
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800760 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700761
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800762 android_namespace_t* ns_not_isolated =
763 android_create_namespace("private", nullptr,
764 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700765 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700766 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
767
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800768 android_namespace_t* ns_isolated =
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700769 android_create_namespace("private_isolated1",
770 nullptr,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800771 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700772 ANDROID_NAMESPACE_TYPE_ISOLATED,
773 nullptr,
774 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700775 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
776
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800777 android_namespace_t* ns_isolated2 =
778 android_create_namespace("private_isolated2",
779 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700780 nullptr,
781 ANDROID_NAMESPACE_TYPE_ISOLATED,
782 lib_path.c_str(),
783 nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700784 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
785
786 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
787 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
788
789 std::string lib_private_external_path =
790 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
791
792 // Load lib_private_external_path to default namespace
793 // (it should remain invisible for the isolated namespaces after this)
794 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
795 ASSERT_TRUE(handle != nullptr) << dlerror();
796
797 android_dlextinfo extinfo;
798 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
799 extinfo.library_namespace = ns_not_isolated;
800
801 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
802 ASSERT_TRUE(handle1 != nullptr) << dlerror();
803
804 extinfo.library_namespace = ns_isolated;
805
806 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
807 ASSERT_TRUE(handle2 == nullptr);
808 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
809
810 // Check dlopen by absolute path
811 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
812 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800813 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700814 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800815 " for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700816
817 extinfo.library_namespace = ns_isolated2;
818
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800819 // this should work because isolation_path for private_isolated2 includes lib_path
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700820 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800821 ASSERT_TRUE(handle2 != nullptr) << dlerror();
822 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700823
824 // Check dlopen by absolute path
825 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800826 ASSERT_TRUE(handle2 != nullptr) << dlerror();
827 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700828
829 typedef const char* (*fn_t)();
830 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
831 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
832
833 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
834
835 fn_t ns_get_private_extern_string =
836 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
837 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
838
839 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
840
841 fn_t ns_get_public_extern_string =
842 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
843 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
844
845 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
846
847 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
848 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
849
850 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
851
852 dlclose(handle1);
853}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800854
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800855TEST(dlext, ns_shared) {
856 static const char* root_lib = "libnstest_root_not_isolated.so";
857 static const char* root_lib_isolated = "libnstest_root.so";
858 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
859
860 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
861 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
862 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
863 ASSERT_TRUE(handle_public != nullptr) << dlerror();
864
865 android_set_application_target_sdk_version(42U); // something > 23
866
867 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
868
869 // preload this library to the default namespace to check if it
870 // is shared later on.
871 void* handle_dlopened =
872 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
873 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
874
875 android_namespace_t* ns_not_isolated =
876 android_create_namespace("private", nullptr,
877 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700878 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800879 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
880
881 android_namespace_t* ns_isolated_shared =
882 android_create_namespace("private_isolated_shared", nullptr,
883 (lib_path + "/private_namespace_libs").c_str(),
884 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700885 nullptr, nullptr);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800886 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
887
888 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
889 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
890
891 std::string lib_private_external_path =
892 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
893
894 // Load lib_private_external_path to default namespace
895 // (it should remain invisible for the isolated namespaces after this)
896 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
897 ASSERT_TRUE(handle != nullptr) << dlerror();
898
899 android_dlextinfo extinfo;
900 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
901 extinfo.library_namespace = ns_not_isolated;
902
903 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
904 ASSERT_TRUE(handle1 != nullptr) << dlerror();
905
906 extinfo.library_namespace = ns_isolated_shared;
907
908 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
909 ASSERT_TRUE(handle2 == nullptr);
910 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
911
912 // Check dlopen by absolute path
913 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
914 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800915 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700916 " or dlopened by \"" + get_executable_path() + "\" is not accessible"
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800917 " for the namespace \"private_isolated_shared\"", dlerror());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800918
919 // load libnstest_root.so to shared namespace in order to check that everything is different
920 // except shared libnstest_dlopened.so
921
922 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
923
924 typedef const char* (*fn_t)();
925 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
926 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
927 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
928 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
929
930 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
931 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
932 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
933
934 fn_t ns_get_private_extern_string =
935 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
936 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
937 fn_t ns_get_private_extern_string_shared =
938 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
939 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
940
941 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
942 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
943 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
944
945 fn_t ns_get_public_extern_string =
946 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
947 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
948 fn_t ns_get_public_extern_string_shared =
949 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
950 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
951
952 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
953 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
954 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
955
956 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
957 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
958 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
959 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
960 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
961 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
962
963 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
964 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
965 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
966 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
967 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
968
969 dlclose(handle1);
970 dlclose(handle2);
971}
972
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700973TEST(dlext, ns_shared_dlclose) {
974 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
975
976 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
977
978 android_set_application_target_sdk_version(42U); // something > 23
979
980 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
981
982 // preload this library to the default namespace to check if it
983 // is shared later on.
984 void* handle_dlopened =
985 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
986 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
987
988 android_namespace_t* ns_isolated_shared =
989 android_create_namespace("private_isolated_shared", nullptr,
990 (lib_path + "/private_namespace_libs").c_str(),
991 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700992 nullptr, nullptr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700993 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
994
995 // Check if "libnstest_dlopened.so" is loaded (and the same)
996 android_dlextinfo extinfo;
997 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
998 extinfo.library_namespace = ns_isolated_shared;
999
1000 void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1001 ASSERT_TRUE(handle != nullptr) << dlerror();
1002 ASSERT_TRUE(handle == handle_dlopened);
1003 dlclose(handle);
1004 dlclose(handle_dlopened);
1005
1006 // And now check that the library cannot be found by soname (and is no longer loaded)
1007 handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
1008 ASSERT_TRUE(handle == nullptr)
1009 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1010
1011 handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1012 RTLD_NOW | RTLD_NOLOAD, &extinfo);
1013 ASSERT_TRUE(handle == nullptr)
1014 << "Error: libnstest_dlopened.so is still accessible in shared namespace";
1015
1016 handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
1017 ASSERT_TRUE(handle == nullptr)
1018 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1019
1020 handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
1021 RTLD_NOW | RTLD_NOLOAD);
1022 ASSERT_TRUE(handle == nullptr)
1023 << "Error: libnstest_dlopened.so is still accessible in default namespace";
1024
1025 // Now lets see if the soinfo area gets reused in the wrong way:
1026 // load a library to default namespace.
1027 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1028 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1029 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1030
1031 // try to find it in shared namespace
1032 handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
1033 ASSERT_TRUE(handle == nullptr)
1034 << "Error: " << g_public_lib << " is accessible in shared namespace";
1035}
1036
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001037TEST(dlext, ns_isolated_rtld_global) {
1038 static const char* root_lib = "libnstest_root.so";
1039 std::string path = "libc.so:libc++.so:libdl.so:libm.so";
1040
1041 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
1042
1043 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
1044
1045 const std::string lib_public_path = lib_path + "/public_namespace_libs";
1046
1047 android_namespace_t* ns1 =
1048 android_create_namespace("isolated1",
1049 nullptr,
1050 (lib_path + "/private_namespace_libs").c_str(),
1051 ANDROID_NAMESPACE_TYPE_ISOLATED,
1052 lib_public_path.c_str(),
1053 nullptr);
1054 ASSERT_TRUE(ns1 != nullptr) << dlerror();
1055
1056 android_namespace_t* ns2 =
1057 android_create_namespace("isolated2",
1058 nullptr,
1059 (lib_path + "/private_namespace_libs").c_str(),
1060 ANDROID_NAMESPACE_TYPE_ISOLATED,
1061 lib_public_path.c_str(),
1062 nullptr);
1063 ASSERT_TRUE(ns2 != nullptr) << dlerror();
1064
1065 android_dlextinfo extinfo;
1066 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1067 extinfo.library_namespace = ns1;
1068
1069 void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
1070 RTLD_GLOBAL,
1071 &extinfo);
1072
1073 ASSERT_TRUE(handle_global != nullptr) << dlerror();
1074
1075 android_namespace_t* ns1_child =
1076 android_create_namespace("isolated1_child",
1077 nullptr,
1078 (lib_path + "/private_namespace_libs").c_str(),
1079 ANDROID_NAMESPACE_TYPE_ISOLATED,
1080 nullptr,
1081 ns1);
1082
1083 // Now - only ns1 and ns1 child should be able to dlopen root_lib
1084 // attempt to use ns2 should result in dlerror()
1085
1086 // Check ns1_child first.
1087 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1088 extinfo.library_namespace = ns1_child;
1089
1090 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1091 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1092
1093 // now ns1
1094 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1095 extinfo.library_namespace = ns1;
1096
1097 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1098 ASSERT_TRUE(handle1 != nullptr) << dlerror();
1099
1100 // and ns2 should fail
1101 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1102 extinfo.library_namespace = ns2;
1103
1104 handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
1105 ASSERT_TRUE(handle1 == nullptr);
1106 ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
1107}
1108
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001109TEST(dlext, ns_anonymous) {
1110 static const char* root_lib = "libnstest_root.so";
1111 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
1112
1113 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
1114
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001115 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
1116 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
1117
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001118 ASSERT_TRUE(handle_public != nullptr) << dlerror();
1119
1120 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
1121 << dlerror();
1122
1123 android_namespace_t* ns = android_create_namespace(
1124 "private", nullptr,
1125 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001126 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08001127
1128 ASSERT_TRUE(ns != nullptr) << dlerror();
1129
1130 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
1131
1132 android_dlextinfo extinfo;
1133 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
1134 extinfo.library_namespace = ns;
1135
1136 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
1137 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
1138 ASSERT_TRUE(handle != nullptr) << dlerror();
1139
1140 uintptr_t ns_get_dlopened_string_addr =
1141 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
1142 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
1143 typedef const char* (*fn_t)();
1144 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
1145
1146 std::vector<map_record> maps;
1147 Maps::parse_maps(&maps);
1148
1149 uintptr_t addr_start = 0;
1150 uintptr_t addr_end = 0;
1151 std::vector<map_record> maps_to_copy;
1152
1153 for (const auto& rec : maps) {
1154 if (rec.pathname == private_library_absolute_path) {
1155 if (addr_start == 0) {
1156 addr_start = rec.addr_start;
1157 }
1158 addr_end = rec.addr_end;
1159
1160 maps_to_copy.push_back(rec);
1161 }
1162 }
1163
1164 // some sanity checks..
1165 ASSERT_TRUE(addr_start > 0);
1166 ASSERT_TRUE(addr_end > 0);
1167 ASSERT_EQ(3U, maps_to_copy.size());
1168 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1169 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1170
1171 // copy
1172 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1173 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1174 -1, 0));
1175 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1176
1177 for (const auto& rec : maps_to_copy) {
1178 uintptr_t offset = rec.addr_start - addr_start;
1179 size_t size = rec.addr_end - rec.addr_start;
1180 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1181 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1182 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1183 ASSERT_TRUE(map != MAP_FAILED);
1184 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1185 mprotect(map, size, rec.perms);
1186 }
1187
1188 // call the function copy
1189 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1190 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1191 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1192 ns_get_dlopened_string_anon());
1193
1194 // They should belong to different namespaces (private and anonymous)
1195 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1196 ns_get_dlopened_string_private());
1197
1198 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1199}
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001200
1201TEST(dlext, dlopen_handle_value_platform) {
1202 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1203 ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
1204 << "dlopen should return odd value for the handle";
1205 dlclose(handle);
1206}
1207
1208TEST(dlext, dlopen_handle_value_app_compat) {
1209 android_set_application_target_sdk_version(23);
1210 void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
1211 ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
1212 << "dlopen should return valid pointer";
1213 dlclose(handle);
1214}
1215