blob: 83bd5cc06814af96c7285d5325ca0b0c340206fc [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>
33
Yabin Cui294d1e22014-12-07 20:43:37 -080034#include "TemporaryFile.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000035
36#define ASSERT_DL_NOTNULL(ptr) \
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070037 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000038
39#define ASSERT_DL_ZERO(i) \
40 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
41
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000042#define ASSERT_NOERROR(i) \
43 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
44
Yabin Cui16f7f8d2014-11-04 11:08:05 -080045#define ASSERT_SUBSTR(needle, haystack) \
46 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
47
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000048
49typedef int (*fn)(void);
50#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010051#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000052#define LIBSIZE 1024*1024 // how much address space to reserve for it
53
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070054#if defined(__LP64__)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070055#define NATIVE_TESTS_PATH "/nativetest64"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070056#else
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070057#define NATIVE_TESTS_PATH "/nativetest"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070058#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000059
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070060#define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
61#define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
62#define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070063
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070064#define LIBZIP_OFFSET PAGE_SIZE
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070065
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000066class DlExtTest : public ::testing::Test {
67protected:
68 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070069 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000070 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070071 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
72 ASSERT_TRUE(h == nullptr);
73 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
74 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000075 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070076 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 +000077 }
78
79 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070080 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000081 ASSERT_DL_ZERO(dlclose(handle_));
82 }
83 }
84
85 void* handle_;
86};
87
88TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070089 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000090 ASSERT_DL_NOTNULL(handle_);
91 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
92 ASSERT_DL_NOTNULL(f);
93 EXPECT_EQ(4, f());
94}
95
96TEST_F(DlExtTest, ExtInfoNoFlags) {
97 android_dlextinfo extinfo;
98 extinfo.flags = 0;
99 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
100 ASSERT_DL_NOTNULL(handle_);
101 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
102 ASSERT_DL_NOTNULL(f);
103 EXPECT_EQ(4, f());
104}
105
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700106TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700107 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700108
109 android_dlextinfo extinfo;
110 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700111 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700112 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700113 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700114 ASSERT_DL_NOTNULL(handle_);
115 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
116 ASSERT_DL_NOTNULL(f);
117 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700118
119 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
120 ASSERT_DL_NOTNULL(taxicab_number);
121 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700122}
123
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700124TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700125 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700126
127 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700128 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700129 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700130 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700131
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700132 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700133 ASSERT_DL_NOTNULL(handle_);
134
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700135 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
136 ASSERT_DL_NOTNULL(taxicab_number);
137 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700138}
139
140TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700141 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700142 // lib_path is relative when $ANDROID_DATA is relative
143 char lib_realpath_buf[PATH_MAX];
144 ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
145 const std::string lib_realpath = std::string(lib_realpath_buf);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700146
147 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700148 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700149 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700150 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700151
152 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
153 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700154 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
155
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800156 // Test an address above 2^44, for http://b/18178121 .
157 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700158 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800160 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
161
162 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
163 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
164 ASSERT_TRUE(handle_ == nullptr);
165 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
166
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700167 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700168 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800169 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700170 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800172 // Check if dlsym works after unsuccessful dlopen().
173 // Supply non-exiting one to make linker visit every soinfo.
174 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
175 ASSERT_TRUE(sym == nullptr);
176
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700177 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700178}
179
180TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
181 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700182 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
183 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700184
185 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
186 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700187 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 -0700188}
189
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700190TEST(dlext, android_dlopen_ext_force_load_smoke) {
191 // 1. Open actual file
192 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
193 ASSERT_DL_NOTNULL(handle);
194 // 2. Open link with force_load flag set
195 android_dlextinfo extinfo;
196 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
197 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
198 ASSERT_DL_NOTNULL(handle2);
199 ASSERT_TRUE(handle != handle2);
200
201 dlclose(handle2);
202 dlclose(handle);
203}
204
205TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
206 // Check if soname lookup still returns already loaded library
207 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
208 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
209 ASSERT_DL_NOTNULL(handle);
210
211 android_dlextinfo extinfo;
212 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
213
214 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
215 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
216
217 ASSERT_DL_NOTNULL(handle2);
218 ASSERT_TRUE(handle == handle2);
219
220 dlclose(handle2);
221 dlclose(handle);
222}
223
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700224TEST(dlfcn, dlopen_from_zip_absolute_path) {
225 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
226
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700227 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700228 ASSERT_TRUE(handle != nullptr) << dlerror();
229
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700230 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
231 ASSERT_DL_NOTNULL(taxicab_number);
232 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700233
234 dlclose(handle);
235}
236
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700237TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
238 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH;
239
240 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
241
242 ASSERT_TRUE(handle != nullptr) << dlerror();
243
244 typedef void *(* dlopen_b_fn)();
245 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
246 ASSERT_TRUE(fn != nullptr) << dlerror();
247
248 void *p = fn();
249 ASSERT_TRUE(p != nullptr) << dlerror();
250
251 dlclose(p);
252 dlclose(handle);
253}
254
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700255TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700256 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700257
258 typedef void (*fn_t)(const char*);
259 fn_t android_update_LD_LIBRARY_PATH =
260 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
261
262 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
263
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700264 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700265 ASSERT_TRUE(handle == nullptr);
266
267 android_update_LD_LIBRARY_PATH(lib_path.c_str());
268
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700269 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700270 ASSERT_TRUE(handle != nullptr) << dlerror();
271
272 int (*fn)(void);
273 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
274 ASSERT_TRUE(fn != nullptr);
275 EXPECT_EQ(4, fn());
276
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700277 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
278 ASSERT_DL_NOTNULL(taxicab_number);
279 EXPECT_EQ(1729U, *taxicab_number);
280
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700281 dlclose(handle);
282}
283
284
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000285TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700286 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000287 -1, 0);
288 ASSERT_TRUE(start != MAP_FAILED);
289 android_dlextinfo extinfo;
290 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
291 extinfo.reserved_addr = start;
292 extinfo.reserved_size = LIBSIZE;
293 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
294 ASSERT_DL_NOTNULL(handle_);
295 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
296 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700297 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000298 EXPECT_LT(reinterpret_cast<void*>(f),
299 reinterpret_cast<char*>(start) + LIBSIZE);
300 EXPECT_EQ(4, f());
301}
302
303TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700304 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000305 -1, 0);
306 ASSERT_TRUE(start != MAP_FAILED);
307 android_dlextinfo extinfo;
308 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
309 extinfo.reserved_addr = start;
310 extinfo.reserved_size = PAGE_SIZE;
311 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700312 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000313}
314
315TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700316 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000317 -1, 0);
318 ASSERT_TRUE(start != MAP_FAILED);
319 android_dlextinfo extinfo;
320 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
321 extinfo.reserved_addr = start;
322 extinfo.reserved_size = LIBSIZE;
323 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
324 ASSERT_DL_NOTNULL(handle_);
325 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
326 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700327 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000328 EXPECT_LT(reinterpret_cast<void*>(f),
329 reinterpret_cast<char*>(start) + LIBSIZE);
330 EXPECT_EQ(4, f());
331}
332
333TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700334 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000335 -1, 0);
336 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 = PAGE_SIZE;
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_TRUE(reinterpret_cast<void*>(f) < start ||
346 (reinterpret_cast<void*>(f) >=
347 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000348 EXPECT_EQ(4, f());
349}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000350
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700351TEST_F(DlExtTest, LoadAtFixedAddress) {
352 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
353 -1, 0);
354 ASSERT_TRUE(start != MAP_FAILED);
355 munmap(start, LIBSIZE);
356
357 android_dlextinfo extinfo;
358 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
359 extinfo.reserved_addr = start;
360
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);
365 EXPECT_GE(reinterpret_cast<void*>(f), start);
366 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
367
368 EXPECT_EQ(4, f());
369}
370
371TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
372 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
373 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
374 ASSERT_TRUE(start != MAP_FAILED);
375 munmap(start, LIBSIZE + PAGE_SIZE);
376 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
377 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
378 ASSERT_TRUE(new_addr != MAP_FAILED);
379
380 android_dlextinfo extinfo;
381 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
382 extinfo.reserved_addr = start;
383
384 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
385 ASSERT_TRUE(handle_ == nullptr);
386}
387
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100388class DlExtRelroSharingTest : public DlExtTest {
389protected:
390 virtual void SetUp() {
391 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700392 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100393 -1, 0);
394 ASSERT_TRUE(start != MAP_FAILED);
395 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
396 extinfo_.reserved_addr = start;
397 extinfo_.reserved_size = LIBSIZE;
398 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000399 }
400
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100401 virtual void TearDown() {
402 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100403 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000404
Yabin Cui294d1e22014-12-07 20:43:37 -0800405 void CreateRelroFile(const char* lib, const char* relro_file) {
406 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100407 ASSERT_NOERROR(relro_fd);
408
409 pid_t pid = fork();
410 if (pid == 0) {
411 // child process
412 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
413 extinfo_.relro_fd = relro_fd;
414 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700415 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100416 fprintf(stderr, "in child: %s\n", dlerror());
417 exit(1);
418 }
419 exit(0);
420 }
421
422 // continuing in parent
423 ASSERT_NOERROR(close(relro_fd));
424 ASSERT_NOERROR(pid);
425 int status;
426 ASSERT_EQ(pid, waitpid(pid, &status, 0));
427 ASSERT_TRUE(WIFEXITED(status));
428 ASSERT_EQ(0, WEXITSTATUS(status));
429
430 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800431 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100432 ASSERT_NOERROR(relro_fd);
433 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
434 extinfo_.relro_fd = relro_fd;
435 }
436
437 void TryUsingRelro(const char* lib) {
438 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
439 ASSERT_DL_NOTNULL(handle_);
440 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
441 ASSERT_DL_NOTNULL(f);
442 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700443
444 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
445 ASSERT_DL_NOTNULL(taxicab_number);
446 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100447 }
448
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100449 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
450
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100451 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100452};
453
454TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800455 TemporaryFile tf; // Use tf to get an unique filename.
456 ASSERT_NOERROR(close(tf.fd));
457
458 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100459 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800460
461 // Use destructor of tf to close and unlink the file.
462 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100463}
464
465TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800466 TemporaryFile tf; // // Use tf to get an unique filename.
467 ASSERT_NOERROR(close(tf.fd));
468
469 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100470 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800471
472 // Use destructor of tf to close and unlink the file.
473 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100474}
475
476TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100477 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000478}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100479
480TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700481 if (geteuid() != 0) {
482 GTEST_LOG_(INFO) << "This test must be run as root.\n";
483 return;
484 }
485
Yabin Cui294d1e22014-12-07 20:43:37 -0800486 TemporaryFile tf; // Use tf to get an unique filename.
487 ASSERT_NOERROR(close(tf.fd));
488
489 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
490
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100491 int pipefd[2];
492 ASSERT_NOERROR(pipe(pipefd));
493
494 size_t without_sharing, with_sharing;
495 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
496 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
497
498 // We expect the sharing to save at least 10% of the total PSS. In practice
499 // it saves 40%+ for this test.
500 size_t expected_size = without_sharing - (without_sharing/10);
501 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800502
503 // Use destructor of tf to close and unlink the file.
504 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100505}
506
507void getPss(pid_t pid, size_t* pss_out) {
508 pm_kernel_t* kernel;
509 ASSERT_EQ(0, pm_kernel_create(&kernel));
510
511 pm_process_t* process;
512 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
513
514 pm_map_t** maps;
515 size_t num_maps;
516 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
517
518 size_t total_pss = 0;
519 for (size_t i = 0; i < num_maps; i++) {
520 pm_memusage_t usage;
521 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
522 total_pss += usage.pss;
523 }
524 *pss_out = total_pss;
525
526 free(maps);
527 pm_process_destroy(process);
528 pm_kernel_destroy(kernel);
529}
530
531void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
532 size_t* pss_out) {
533 const int CHILDREN = 20;
534
535 // Create children
536 pid_t childpid[CHILDREN];
537 int childpipe[CHILDREN];
538 for (int i=0; i<CHILDREN; ++i) {
539 char read_buf;
540 int child_done_pipe[2], parent_done_pipe[2];
541 ASSERT_NOERROR(pipe(child_done_pipe));
542 ASSERT_NOERROR(pipe(parent_done_pipe));
543
544 pid_t child = fork();
545 if (child == 0) {
546 // close the 'wrong' ends of the pipes in the child
547 close(child_done_pipe[0]);
548 close(parent_done_pipe[1]);
549
550 // open the library
551 void* handle;
552 if (share_relro) {
553 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
554 } else {
555 handle = dlopen(lib, RTLD_NOW);
556 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700557 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100558 fprintf(stderr, "in child: %s\n", dlerror());
559 exit(1);
560 }
561
562 // close write end of child_done_pipe to signal the parent that we're done.
563 close(child_done_pipe[1]);
564
565 // wait for the parent to close parent_done_pipe, then exit
566 read(parent_done_pipe[0], &read_buf, 1);
567 exit(0);
568 }
569
570 ASSERT_NOERROR(child);
571
572 // close the 'wrong' ends of the pipes in the parent
573 close(child_done_pipe[1]);
574 close(parent_done_pipe[0]);
575
576 // wait for the child to be done
577 read(child_done_pipe[0], &read_buf, 1);
578 close(child_done_pipe[0]);
579
580 // save the child's pid and the parent_done_pipe
581 childpid[i] = child;
582 childpipe[i] = parent_done_pipe[1];
583 }
584
585 // Sum the PSS of all the children
586 size_t total_pss = 0;
587 for (int i=0; i<CHILDREN; ++i) {
588 size_t child_pss;
589 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
590 total_pss += child_pss;
591 }
592 *pss_out = total_pss;
593
594 // Close pipes and wait for children to exit
595 for (int i=0; i<CHILDREN; ++i) {
596 ASSERT_NOERROR(close(childpipe[i]));
597 }
598 for (int i=0; i<CHILDREN; ++i) {
599 int status;
600 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
601 ASSERT_TRUE(WIFEXITED(status));
602 ASSERT_EQ(0, WEXITSTATUS(status));
603 }
604}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700605
606// Testing namespaces
607static const char* g_public_lib = "libnstest_public.so";
608
609TEST(dlext, ns_smoke) {
610 static const char* root_lib = "libnstest_root.so";
611 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
612
613 ASSERT_FALSE(android_init_public_namespace(path.c_str()));
614 ASSERT_STREQ("android_init_public_namespace failed: Error initializing public namespace: "
615 "\"libnstest_public.so\" was not found in the default namespace", dlerror());
616
617 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
618
619 void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW);
620 ASSERT_TRUE(handle_public != nullptr) << dlerror();
621
622 ASSERT_TRUE(android_init_public_namespace(path.c_str())) << dlerror();
623
624 // Check that libraries added to public namespace are NODELETE
625 dlclose(handle_public);
626 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW | RTLD_NOLOAD);
627 ASSERT_TRUE(handle_public != nullptr) << dlerror();
628
629 android_namespace_t* ns1 = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false);
630 ASSERT_TRUE(ns1 != nullptr) << dlerror();
631
632 android_namespace_t* ns2 = android_create_namespace("private_isolated", nullptr, (lib_path + "/private_namespace_libs").c_str(), true);
633 ASSERT_TRUE(ns2 != nullptr) << dlerror();
634
635 // This should not have affect search path for default namespace:
636 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
637 void* handle = dlopen(g_public_lib, RTLD_NOW);
638 ASSERT_TRUE(handle != nullptr) << dlerror();
639 dlclose(handle);
640
641 android_dlextinfo extinfo;
642 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
643 extinfo.library_namespace = ns1;
644
645 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
646 ASSERT_TRUE(handle1 != nullptr) << dlerror();
647
648 extinfo.library_namespace = ns2;
649 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
650 ASSERT_TRUE(handle2 != nullptr) << dlerror();
651
652 ASSERT_TRUE(handle1 != handle2);
653
654 typedef const char* (*fn_t)();
655
656 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
657 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
658 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
659 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
660
661 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
662 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
663
664 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
665
666 fn_t ns_get_private_extern_string1 =
667 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
668 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
669 fn_t ns_get_private_extern_string2 =
670 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
671 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
672
673 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
674 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
675
676 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
677
678 fn_t ns_get_public_extern_string1 =
679 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
680 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
681 fn_t ns_get_public_extern_string2 =
682 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
683 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
684
685 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
686 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
687
688 // and now check that dlopen() does the right thing in terms of preserving namespace
689 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
690 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
691 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
692 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
693
694 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
695 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
696
697 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
698
699 dlclose(handle1);
700
701 // Check if handle2 is still alive (and well)
702 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
703 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
704 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
705 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
706
707 dlclose(handle2);
708}
709
710TEST(dlext, ns_isolated) {
711 static const char* root_lib = "libnstest_root_not_isolated.so";
712 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
713
714 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
715 void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW);
716 ASSERT_TRUE(handle_public != nullptr) << dlerror();
717
718 ASSERT_TRUE(android_init_public_namespace(path.c_str())) << dlerror();
719
720 android_namespace_t* ns_not_isolated = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false);
721 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
722
723 android_namespace_t* ns_isolated = android_create_namespace("private_isolated1", nullptr, (lib_path + "/private_namespace_libs").c_str(), true);
724 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
725
726 android_namespace_t* ns_isolated2 = android_create_namespace("private_isolated2", (lib_path + "/private_namespace_libs").c_str(), nullptr, true);
727 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
728
729 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
730 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
731
732 std::string lib_private_external_path =
733 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
734
735 // Load lib_private_external_path to default namespace
736 // (it should remain invisible for the isolated namespaces after this)
737 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
738 ASSERT_TRUE(handle != nullptr) << dlerror();
739
740 android_dlextinfo extinfo;
741 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
742 extinfo.library_namespace = ns_not_isolated;
743
744 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
745 ASSERT_TRUE(handle1 != nullptr) << dlerror();
746
747 extinfo.library_namespace = ns_isolated;
748
749 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
750 ASSERT_TRUE(handle2 == nullptr);
751 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
752
753 // Check dlopen by absolute path
754 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
755 ASSERT_TRUE(handle2 == nullptr);
756 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror());
757
758 extinfo.library_namespace = ns_isolated2;
759
760 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
761 ASSERT_TRUE(handle2 == nullptr);
762 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
763
764 // Check dlopen by absolute path
765 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
766 ASSERT_TRUE(handle2 == nullptr);
767 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror());
768
769 typedef const char* (*fn_t)();
770 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
771 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
772
773 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
774
775 fn_t ns_get_private_extern_string =
776 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
777 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
778
779 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
780
781 fn_t ns_get_public_extern_string =
782 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
783 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
784
785 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
786
787 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
788 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
789
790 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
791
792 dlclose(handle1);
793}