blob: 83791ebb2c5c2e2a789813a1aaa8d1f4220075f4 [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"
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080035#include "utils.h"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000036
37#define ASSERT_DL_NOTNULL(ptr) \
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070038 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000039
40#define ASSERT_DL_ZERO(i) \
41 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
42
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000043#define ASSERT_NOERROR(i) \
44 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
45
Yabin Cui16f7f8d2014-11-04 11:08:05 -080046#define ASSERT_SUBSTR(needle, haystack) \
47 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
48
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000049
50typedef int (*fn)(void);
51#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010052#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053#define LIBSIZE 1024*1024 // how much address space to reserve for it
54
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070055#if defined(__LP64__)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070056#define NATIVE_TESTS_PATH "/nativetest64"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070057#else
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070058#define NATIVE_TESTS_PATH "/nativetest"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070059#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000060
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070061#define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
62#define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
63#define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070064
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070065#define LIBZIP_OFFSET PAGE_SIZE
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070066
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000067class DlExtTest : public ::testing::Test {
68protected:
69 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070070 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000071 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070072 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
73 ASSERT_TRUE(h == nullptr);
74 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
75 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000076 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070077 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 +000078 }
79
80 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070081 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000082 ASSERT_DL_ZERO(dlclose(handle_));
83 }
84 }
85
86 void* handle_;
87};
88
89TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070090 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000091 ASSERT_DL_NOTNULL(handle_);
92 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
93 ASSERT_DL_NOTNULL(f);
94 EXPECT_EQ(4, f());
95}
96
97TEST_F(DlExtTest, ExtInfoNoFlags) {
98 android_dlextinfo extinfo;
99 extinfo.flags = 0;
100 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
101 ASSERT_DL_NOTNULL(handle_);
102 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
103 ASSERT_DL_NOTNULL(f);
104 EXPECT_EQ(4, f());
105}
106
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700107TEST_F(DlExtTest, ExtInfoUseFd) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700108 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700109
110 android_dlextinfo extinfo;
111 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700112 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700113 ASSERT_TRUE(extinfo.library_fd != -1);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700114 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700115 ASSERT_DL_NOTNULL(handle_);
116 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
117 ASSERT_DL_NOTNULL(f);
118 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700119
120 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
121 ASSERT_DL_NOTNULL(taxicab_number);
122 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700123}
124
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700125TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700126 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700127
128 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700129 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700130 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700131 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700132
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700133 handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700134 ASSERT_DL_NOTNULL(handle_);
135
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700136 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
137 ASSERT_DL_NOTNULL(taxicab_number);
138 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700139}
140
141TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700142 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700143 // lib_path is relative when $ANDROID_DATA is relative
144 char lib_realpath_buf[PATH_MAX];
145 ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
146 const std::string lib_realpath = std::string(lib_realpath_buf);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700147
148 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700149 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700150 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700151 extinfo.library_fd_offset = 17;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700152
153 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
154 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700155 ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
156
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800157 // Test an address above 2^44, for http://b/18178121 .
158 extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700159 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700160 ASSERT_TRUE(handle_ == nullptr);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800161 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
162
163 extinfo.library_fd_offset = 0LL - PAGE_SIZE;
164 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
165 ASSERT_TRUE(handle_ == nullptr);
166 ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
167
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700168 extinfo.library_fd_offset = 0;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700169 handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800170 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanovef255922015-04-08 11:53:08 -0700171 ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700172
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -0800173 // Check if dlsym works after unsuccessful dlopen().
174 // Supply non-exiting one to make linker visit every soinfo.
175 void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
176 ASSERT_TRUE(sym == nullptr);
177
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700178 close(extinfo.library_fd);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700179}
180
181TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
182 android_dlextinfo extinfo;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700183 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
184 extinfo.library_fd_offset = LIBZIP_OFFSET;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700185
186 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
187 ASSERT_TRUE(handle_ == nullptr);
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700188 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 -0700189}
190
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700191TEST(dlext, android_dlopen_ext_force_load_smoke) {
192 // 1. Open actual file
193 void* handle = dlopen("libdlext_test.so", RTLD_NOW);
194 ASSERT_DL_NOTNULL(handle);
195 // 2. Open link with force_load flag set
196 android_dlextinfo extinfo;
197 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
198 void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
199 ASSERT_DL_NOTNULL(handle2);
200 ASSERT_TRUE(handle != handle2);
201
202 dlclose(handle2);
203 dlclose(handle);
204}
205
206TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
207 // Check if soname lookup still returns already loaded library
208 // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
209 void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
210 ASSERT_DL_NOTNULL(handle);
211
212 android_dlextinfo extinfo;
213 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
214
215 // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
216 void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
217
218 ASSERT_DL_NOTNULL(handle2);
219 ASSERT_TRUE(handle == handle2);
220
221 dlclose(handle2);
222 dlclose(handle);
223}
224
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700225TEST(dlfcn, dlopen_from_zip_absolute_path) {
226 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
227
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700228 void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700229 ASSERT_TRUE(handle != nullptr) << dlerror();
230
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700231 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
232 ASSERT_DL_NOTNULL(taxicab_number);
233 EXPECT_EQ(1729U, *taxicab_number);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700234
235 dlclose(handle);
236}
237
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700238TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
239 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH;
240
241 void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
242
243 ASSERT_TRUE(handle != nullptr) << dlerror();
244
245 typedef void *(* dlopen_b_fn)();
246 dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
247 ASSERT_TRUE(fn != nullptr) << dlerror();
248
249 void *p = fn();
250 ASSERT_TRUE(p != nullptr) << dlerror();
251
252 dlclose(p);
253 dlclose(handle);
254}
255
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700256TEST(dlfcn, dlopen_from_zip_ld_library_path) {
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700257 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700258
259 typedef void (*fn_t)(const char*);
260 fn_t android_update_LD_LIBRARY_PATH =
261 reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
262
263 ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
264
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700265 void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700266 ASSERT_TRUE(handle == nullptr);
267
268 android_update_LD_LIBRARY_PATH(lib_path.c_str());
269
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700270 handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700271 ASSERT_TRUE(handle != nullptr) << dlerror();
272
273 int (*fn)(void);
274 fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
275 ASSERT_TRUE(fn != nullptr);
276 EXPECT_EQ(4, fn());
277
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700278 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
279 ASSERT_DL_NOTNULL(taxicab_number);
280 EXPECT_EQ(1729U, *taxicab_number);
281
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700282 dlclose(handle);
283}
284
285
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000286TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700287 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000288 -1, 0);
289 ASSERT_TRUE(start != MAP_FAILED);
290 android_dlextinfo extinfo;
291 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
292 extinfo.reserved_addr = start;
293 extinfo.reserved_size = LIBSIZE;
294 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
295 ASSERT_DL_NOTNULL(handle_);
296 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
297 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700298 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000299 EXPECT_LT(reinterpret_cast<void*>(f),
300 reinterpret_cast<char*>(start) + LIBSIZE);
301 EXPECT_EQ(4, f());
302}
303
304TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700305 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000306 -1, 0);
307 ASSERT_TRUE(start != MAP_FAILED);
308 android_dlextinfo extinfo;
309 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
310 extinfo.reserved_addr = start;
311 extinfo.reserved_size = PAGE_SIZE;
312 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700313 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000314}
315
316TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700317 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000318 -1, 0);
319 ASSERT_TRUE(start != MAP_FAILED);
320 android_dlextinfo extinfo;
321 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
322 extinfo.reserved_addr = start;
323 extinfo.reserved_size = LIBSIZE;
324 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
325 ASSERT_DL_NOTNULL(handle_);
326 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
327 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700328 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000329 EXPECT_LT(reinterpret_cast<void*>(f),
330 reinterpret_cast<char*>(start) + LIBSIZE);
331 EXPECT_EQ(4, f());
332}
333
334TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700335 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000336 -1, 0);
337 ASSERT_TRUE(start != MAP_FAILED);
338 android_dlextinfo extinfo;
339 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
340 extinfo.reserved_addr = start;
341 extinfo.reserved_size = PAGE_SIZE;
342 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
343 ASSERT_DL_NOTNULL(handle_);
344 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
345 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700346 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
347 (reinterpret_cast<void*>(f) >=
348 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000349 EXPECT_EQ(4, f());
350}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000351
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700352TEST_F(DlExtTest, LoadAtFixedAddress) {
353 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
354 -1, 0);
355 ASSERT_TRUE(start != MAP_FAILED);
356 munmap(start, LIBSIZE);
357
358 android_dlextinfo extinfo;
359 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
360 extinfo.reserved_addr = start;
361
362 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
363 ASSERT_DL_NOTNULL(handle_);
364 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
365 ASSERT_DL_NOTNULL(f);
366 EXPECT_GE(reinterpret_cast<void*>(f), start);
367 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
368
369 EXPECT_EQ(4, f());
370}
371
372TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
373 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
374 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
375 ASSERT_TRUE(start != MAP_FAILED);
376 munmap(start, LIBSIZE + PAGE_SIZE);
377 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
378 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
379 ASSERT_TRUE(new_addr != MAP_FAILED);
380
381 android_dlextinfo extinfo;
382 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
383 extinfo.reserved_addr = start;
384
385 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
386 ASSERT_TRUE(handle_ == nullptr);
387}
388
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100389class DlExtRelroSharingTest : public DlExtTest {
390protected:
391 virtual void SetUp() {
392 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700393 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100394 -1, 0);
395 ASSERT_TRUE(start != MAP_FAILED);
396 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
397 extinfo_.reserved_addr = start;
398 extinfo_.reserved_size = LIBSIZE;
399 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000400 }
401
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100402 virtual void TearDown() {
403 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100404 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000405
Yabin Cui294d1e22014-12-07 20:43:37 -0800406 void CreateRelroFile(const char* lib, const char* relro_file) {
407 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100408 ASSERT_NOERROR(relro_fd);
409
410 pid_t pid = fork();
411 if (pid == 0) {
412 // child process
413 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
414 extinfo_.relro_fd = relro_fd;
415 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700416 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100417 fprintf(stderr, "in child: %s\n", dlerror());
418 exit(1);
419 }
420 exit(0);
421 }
422
423 // continuing in parent
424 ASSERT_NOERROR(close(relro_fd));
425 ASSERT_NOERROR(pid);
426 int status;
427 ASSERT_EQ(pid, waitpid(pid, &status, 0));
428 ASSERT_TRUE(WIFEXITED(status));
429 ASSERT_EQ(0, WEXITSTATUS(status));
430
431 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800432 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100433 ASSERT_NOERROR(relro_fd);
434 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
435 extinfo_.relro_fd = relro_fd;
436 }
437
438 void TryUsingRelro(const char* lib) {
439 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
440 ASSERT_DL_NOTNULL(handle_);
441 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
442 ASSERT_DL_NOTNULL(f);
443 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700444
445 uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
446 ASSERT_DL_NOTNULL(taxicab_number);
447 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100448 }
449
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100450 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
451
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100452 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100453};
454
455TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800456 TemporaryFile tf; // Use tf to get an unique filename.
457 ASSERT_NOERROR(close(tf.fd));
458
459 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100460 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800461
462 // Use destructor of tf to close and unlink the file.
463 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100464}
465
466TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800467 TemporaryFile tf; // // Use tf to get an unique filename.
468 ASSERT_NOERROR(close(tf.fd));
469
470 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100471 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800472
473 // Use destructor of tf to close and unlink the file.
474 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100475}
476
477TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100478 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000479}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100480
481TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700482 if (geteuid() != 0) {
483 GTEST_LOG_(INFO) << "This test must be run as root.\n";
484 return;
485 }
486
Yabin Cui294d1e22014-12-07 20:43:37 -0800487 TemporaryFile tf; // Use tf to get an unique filename.
488 ASSERT_NOERROR(close(tf.fd));
489
490 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
491
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100492 int pipefd[2];
493 ASSERT_NOERROR(pipe(pipefd));
494
495 size_t without_sharing, with_sharing;
496 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
497 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
498
499 // We expect the sharing to save at least 10% of the total PSS. In practice
500 // it saves 40%+ for this test.
501 size_t expected_size = without_sharing - (without_sharing/10);
502 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800503
504 // Use destructor of tf to close and unlink the file.
505 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100506}
507
508void getPss(pid_t pid, size_t* pss_out) {
509 pm_kernel_t* kernel;
510 ASSERT_EQ(0, pm_kernel_create(&kernel));
511
512 pm_process_t* process;
513 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
514
515 pm_map_t** maps;
516 size_t num_maps;
517 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
518
519 size_t total_pss = 0;
520 for (size_t i = 0; i < num_maps; i++) {
521 pm_memusage_t usage;
522 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
523 total_pss += usage.pss;
524 }
525 *pss_out = total_pss;
526
527 free(maps);
528 pm_process_destroy(process);
529 pm_kernel_destroy(kernel);
530}
531
532void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
533 size_t* pss_out) {
534 const int CHILDREN = 20;
535
536 // Create children
537 pid_t childpid[CHILDREN];
538 int childpipe[CHILDREN];
539 for (int i=0; i<CHILDREN; ++i) {
540 char read_buf;
541 int child_done_pipe[2], parent_done_pipe[2];
542 ASSERT_NOERROR(pipe(child_done_pipe));
543 ASSERT_NOERROR(pipe(parent_done_pipe));
544
545 pid_t child = fork();
546 if (child == 0) {
547 // close the 'wrong' ends of the pipes in the child
548 close(child_done_pipe[0]);
549 close(parent_done_pipe[1]);
550
551 // open the library
552 void* handle;
553 if (share_relro) {
554 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
555 } else {
556 handle = dlopen(lib, RTLD_NOW);
557 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700558 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100559 fprintf(stderr, "in child: %s\n", dlerror());
560 exit(1);
561 }
562
563 // close write end of child_done_pipe to signal the parent that we're done.
564 close(child_done_pipe[1]);
565
566 // wait for the parent to close parent_done_pipe, then exit
567 read(parent_done_pipe[0], &read_buf, 1);
568 exit(0);
569 }
570
571 ASSERT_NOERROR(child);
572
573 // close the 'wrong' ends of the pipes in the parent
574 close(child_done_pipe[1]);
575 close(parent_done_pipe[0]);
576
577 // wait for the child to be done
578 read(child_done_pipe[0], &read_buf, 1);
579 close(child_done_pipe[0]);
580
581 // save the child's pid and the parent_done_pipe
582 childpid[i] = child;
583 childpipe[i] = parent_done_pipe[1];
584 }
585
586 // Sum the PSS of all the children
587 size_t total_pss = 0;
588 for (int i=0; i<CHILDREN; ++i) {
589 size_t child_pss;
590 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
591 total_pss += child_pss;
592 }
593 *pss_out = total_pss;
594
595 // Close pipes and wait for children to exit
596 for (int i=0; i<CHILDREN; ++i) {
597 ASSERT_NOERROR(close(childpipe[i]));
598 }
599 for (int i=0; i<CHILDREN; ++i) {
600 int status;
601 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
602 ASSERT_TRUE(WIFEXITED(status));
603 ASSERT_EQ(0, WEXITSTATUS(status));
604 }
605}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700606
607// Testing namespaces
608static const char* g_public_lib = "libnstest_public.so";
609
610TEST(dlext, ns_smoke) {
611 static const char* root_lib = "libnstest_root.so";
612 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
613
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800614 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
615 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700616 "\"libnstest_public.so\" was not found in the default namespace", dlerror());
617
618 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
619
620 void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW);
621 ASSERT_TRUE(handle_public != nullptr) << dlerror();
622
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800623 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700624
625 // Check that libraries added to public namespace are NODELETE
626 dlclose(handle_public);
627 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW | RTLD_NOLOAD);
628 ASSERT_TRUE(handle_public != nullptr) << dlerror();
629
630 android_namespace_t* ns1 = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false);
631 ASSERT_TRUE(ns1 != nullptr) << dlerror();
632
633 android_namespace_t* ns2 = android_create_namespace("private_isolated", nullptr, (lib_path + "/private_namespace_libs").c_str(), true);
634 ASSERT_TRUE(ns2 != nullptr) << dlerror();
635
636 // This should not have affect search path for default namespace:
637 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
638 void* handle = dlopen(g_public_lib, RTLD_NOW);
639 ASSERT_TRUE(handle != nullptr) << dlerror();
640 dlclose(handle);
641
642 android_dlextinfo extinfo;
643 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
644 extinfo.library_namespace = ns1;
645
646 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
647 ASSERT_TRUE(handle1 != nullptr) << dlerror();
648
649 extinfo.library_namespace = ns2;
650 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
651 ASSERT_TRUE(handle2 != nullptr) << dlerror();
652
653 ASSERT_TRUE(handle1 != handle2);
654
655 typedef const char* (*fn_t)();
656
657 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
658 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
659 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
660 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
661
662 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
663 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
664
665 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
666
667 fn_t ns_get_private_extern_string1 =
668 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
669 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
670 fn_t ns_get_private_extern_string2 =
671 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
672 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
673
674 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
675 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
676
677 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
678
679 fn_t ns_get_public_extern_string1 =
680 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
681 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
682 fn_t ns_get_public_extern_string2 =
683 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
684 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
685
686 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
687 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
688
689 // and now check that dlopen() does the right thing in terms of preserving namespace
690 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
691 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
692 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
693 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
694
695 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
696 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
697
698 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
699
700 dlclose(handle1);
701
702 // Check if handle2 is still alive (and well)
703 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
704 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
705 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
706 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
707
708 dlclose(handle2);
709}
710
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800711extern "C" void android_set_application_target_sdk_version(uint32_t target);
712
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700713TEST(dlext, ns_isolated) {
714 static const char* root_lib = "libnstest_root_not_isolated.so";
715 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
716
717 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
718 void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW);
719 ASSERT_TRUE(handle_public != nullptr) << dlerror();
720
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800721 android_set_application_target_sdk_version(42U); // something > 23
722
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800723 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700724
725 android_namespace_t* ns_not_isolated = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false);
726 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
727
728 android_namespace_t* ns_isolated = android_create_namespace("private_isolated1", nullptr, (lib_path + "/private_namespace_libs").c_str(), true);
729 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
730
731 android_namespace_t* ns_isolated2 = android_create_namespace("private_isolated2", (lib_path + "/private_namespace_libs").c_str(), nullptr, true);
732 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
733
734 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
735 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
736
737 std::string lib_private_external_path =
738 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
739
740 // Load lib_private_external_path to default namespace
741 // (it should remain invisible for the isolated namespaces after this)
742 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
743 ASSERT_TRUE(handle != nullptr) << dlerror();
744
745 android_dlextinfo extinfo;
746 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
747 extinfo.library_namespace = ns_not_isolated;
748
749 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
750 ASSERT_TRUE(handle1 != nullptr) << dlerror();
751
752 extinfo.library_namespace = ns_isolated;
753
754 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
755 ASSERT_TRUE(handle2 == nullptr);
756 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
757
758 // Check dlopen by absolute path
759 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
760 ASSERT_TRUE(handle2 == nullptr);
761 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror());
762
763 extinfo.library_namespace = ns_isolated2;
764
765 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
766 ASSERT_TRUE(handle2 == nullptr);
767 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
768
769 // Check dlopen by absolute path
770 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
771 ASSERT_TRUE(handle2 == nullptr);
772 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" not found", dlerror());
773
774 typedef const char* (*fn_t)();
775 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
776 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
777
778 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
779
780 fn_t ns_get_private_extern_string =
781 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
782 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
783
784 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
785
786 fn_t ns_get_public_extern_string =
787 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
788 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
789
790 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
791
792 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
793 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
794
795 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
796
797 dlclose(handle1);
798}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800799
800TEST(dlext, ns_anonymous) {
801 static const char* root_lib = "libnstest_root.so";
802 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
803
804 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
805
806 void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
807 RTLD_NOW);
808 ASSERT_TRUE(handle_public != nullptr) << dlerror();
809
810 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
811 << dlerror();
812
813 android_namespace_t* ns = android_create_namespace(
814 "private", nullptr,
815 (lib_path + "/private_namespace_libs").c_str(),
816 false);
817
818 ASSERT_TRUE(ns != nullptr) << dlerror();
819
820 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
821
822 android_dlextinfo extinfo;
823 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
824 extinfo.library_namespace = ns;
825
826 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
827 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
828 ASSERT_TRUE(handle != nullptr) << dlerror();
829
830 uintptr_t ns_get_dlopened_string_addr =
831 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
832 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
833 typedef const char* (*fn_t)();
834 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
835
836 std::vector<map_record> maps;
837 Maps::parse_maps(&maps);
838
839 uintptr_t addr_start = 0;
840 uintptr_t addr_end = 0;
841 std::vector<map_record> maps_to_copy;
842
843 for (const auto& rec : maps) {
844 if (rec.pathname == private_library_absolute_path) {
845 if (addr_start == 0) {
846 addr_start = rec.addr_start;
847 }
848 addr_end = rec.addr_end;
849
850 maps_to_copy.push_back(rec);
851 }
852 }
853
854 // some sanity checks..
855 ASSERT_TRUE(addr_start > 0);
856 ASSERT_TRUE(addr_end > 0);
857 ASSERT_EQ(3U, maps_to_copy.size());
858 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
859 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
860
861 // copy
862 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
863 PROT_NONE, MAP_ANON | MAP_PRIVATE,
864 -1, 0));
865 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
866
867 for (const auto& rec : maps_to_copy) {
868 uintptr_t offset = rec.addr_start - addr_start;
869 size_t size = rec.addr_end - rec.addr_start;
870 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
871 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
872 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
873 ASSERT_TRUE(map != MAP_FAILED);
874 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
875 mprotect(map, size, rec.perms);
876 }
877
878 // call the function copy
879 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
880 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
881 ASSERT_STREQ("This string is from private namespace (dlopened library)",
882 ns_get_dlopened_string_anon());
883
884 // They should belong to different namespaces (private and anonymous)
885 ASSERT_STREQ("This string is from private namespace (dlopened library)",
886 ns_get_dlopened_string_private());
887
888 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
889}