blob: 261aa55492b30b1be7a1ff8e6aa453a3d77d651a [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
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800620 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
621 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700622 ASSERT_TRUE(handle_public != nullptr) << dlerror();
623
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800624 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700625
626 // Check that libraries added to public namespace are NODELETE
627 dlclose(handle_public);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800628 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
629 RTLD_NOW | RTLD_NOLOAD);
630
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700631 ASSERT_TRUE(handle_public != nullptr) << dlerror();
632
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800633 android_namespace_t* ns1 =
634 android_create_namespace("private", nullptr,
635 (lib_path + "/private_namespace_libs").c_str(),
636 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700637 ASSERT_TRUE(ns1 != nullptr) << dlerror();
638
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800639 android_namespace_t* ns2 =
640 android_create_namespace("private_isolated", nullptr,
641 (lib_path + "/private_namespace_libs").c_str(),
642 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700643 ASSERT_TRUE(ns2 != nullptr) << dlerror();
644
645 // This should not have affect search path for default namespace:
646 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
647 void* handle = dlopen(g_public_lib, RTLD_NOW);
648 ASSERT_TRUE(handle != nullptr) << dlerror();
649 dlclose(handle);
650
651 android_dlextinfo extinfo;
652 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
653 extinfo.library_namespace = ns1;
654
655 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
656 ASSERT_TRUE(handle1 != nullptr) << dlerror();
657
658 extinfo.library_namespace = ns2;
659 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
660 ASSERT_TRUE(handle2 != nullptr) << dlerror();
661
662 ASSERT_TRUE(handle1 != handle2);
663
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800664 // dlopen for a public library using an absolute path should work for isolated namespaces
665 extinfo.library_namespace = ns2;
666 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
667 ASSERT_TRUE(handle != nullptr) << dlerror();
668 ASSERT_TRUE(handle == handle_public);
669
670 dlclose(handle);
671
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700672 typedef const char* (*fn_t)();
673
674 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
675 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
676 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
677 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
678
679 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
680 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
681
682 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
683
684 fn_t ns_get_private_extern_string1 =
685 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
686 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
687 fn_t ns_get_private_extern_string2 =
688 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
689 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
690
691 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
692 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
693
694 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
695
696 fn_t ns_get_public_extern_string1 =
697 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
698 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
699 fn_t ns_get_public_extern_string2 =
700 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
701 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
702
703 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
704 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
705
706 // and now check that dlopen() does the right thing in terms of preserving namespace
707 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
708 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
709 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
710 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
711
712 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
713 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
714
715 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
716
717 dlclose(handle1);
718
719 // Check if handle2 is still alive (and well)
720 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
721 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
722 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
723 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
724
725 dlclose(handle2);
726}
727
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800728extern "C" void android_set_application_target_sdk_version(uint32_t target);
729
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700730TEST(dlext, ns_isolated) {
731 static const char* root_lib = "libnstest_root_not_isolated.so";
732 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
733
734 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800735 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
736 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700737 ASSERT_TRUE(handle_public != nullptr) << dlerror();
738
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800739 android_set_application_target_sdk_version(42U); // something > 23
740
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800741 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700742
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800743 android_namespace_t* ns_not_isolated =
744 android_create_namespace("private", nullptr,
745 (lib_path + "/private_namespace_libs").c_str(),
746 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700747 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
748
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800749 android_namespace_t* ns_isolated =
750 android_create_namespace("private_isolated1", nullptr,
751 (lib_path + "/private_namespace_libs").c_str(),
752 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700753 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
754
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800755 android_namespace_t* ns_isolated2 =
756 android_create_namespace("private_isolated2",
757 (lib_path + "/private_namespace_libs").c_str(),
758 nullptr, ANDROID_NAMESPACE_TYPE_ISOLATED, lib_path.c_str());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700759 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
760
761 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
762 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
763
764 std::string lib_private_external_path =
765 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
766
767 // Load lib_private_external_path to default namespace
768 // (it should remain invisible for the isolated namespaces after this)
769 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
770 ASSERT_TRUE(handle != nullptr) << dlerror();
771
772 android_dlextinfo extinfo;
773 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
774 extinfo.library_namespace = ns_not_isolated;
775
776 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
777 ASSERT_TRUE(handle1 != nullptr) << dlerror();
778
779 extinfo.library_namespace = ns_isolated;
780
781 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
782 ASSERT_TRUE(handle2 == nullptr);
783 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
784
785 // Check dlopen by absolute path
786 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
787 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800788 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" is not accessible for the namespace \"private_isolated1\"", dlerror());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700789
790 extinfo.library_namespace = ns_isolated2;
791
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800792 // this should work because isolation_path for private_isolated2 includes lib_path
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700793 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800794 ASSERT_TRUE(handle2 != nullptr) << dlerror();
795 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700796
797 // Check dlopen by absolute path
798 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800799 ASSERT_TRUE(handle2 != nullptr) << dlerror();
800 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700801
802 typedef const char* (*fn_t)();
803 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
804 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
805
806 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
807
808 fn_t ns_get_private_extern_string =
809 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
810 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
811
812 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
813
814 fn_t ns_get_public_extern_string =
815 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
816 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
817
818 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
819
820 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
821 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
822
823 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
824
825 dlclose(handle1);
826}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800827
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800828TEST(dlext, ns_shared) {
829 static const char* root_lib = "libnstest_root_not_isolated.so";
830 static const char* root_lib_isolated = "libnstest_root.so";
831 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
832
833 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
834 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
835 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
836 ASSERT_TRUE(handle_public != nullptr) << dlerror();
837
838 android_set_application_target_sdk_version(42U); // something > 23
839
840 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
841
842 // preload this library to the default namespace to check if it
843 // is shared later on.
844 void* handle_dlopened =
845 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
846 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
847
848 android_namespace_t* ns_not_isolated =
849 android_create_namespace("private", nullptr,
850 (lib_path + "/private_namespace_libs").c_str(),
851 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
852 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
853
854 android_namespace_t* ns_isolated_shared =
855 android_create_namespace("private_isolated_shared", nullptr,
856 (lib_path + "/private_namespace_libs").c_str(),
857 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
858 nullptr);
859 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
860
861 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
862 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
863
864 std::string lib_private_external_path =
865 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
866
867 // Load lib_private_external_path to default namespace
868 // (it should remain invisible for the isolated namespaces after this)
869 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
870 ASSERT_TRUE(handle != nullptr) << dlerror();
871
872 android_dlextinfo extinfo;
873 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
874 extinfo.library_namespace = ns_not_isolated;
875
876 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
877 ASSERT_TRUE(handle1 != nullptr) << dlerror();
878
879 extinfo.library_namespace = ns_isolated_shared;
880
881 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
882 ASSERT_TRUE(handle2 == nullptr);
883 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
884
885 // Check dlopen by absolute path
886 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
887 ASSERT_TRUE(handle2 == nullptr);
888 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" is not accessible for the namespace \"private_isolated_shared\"", dlerror());
889
890 // load libnstest_root.so to shared namespace in order to check that everything is different
891 // except shared libnstest_dlopened.so
892
893 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
894
895 typedef const char* (*fn_t)();
896 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
897 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
898 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
899 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
900
901 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
902 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
903 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
904
905 fn_t ns_get_private_extern_string =
906 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
907 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
908 fn_t ns_get_private_extern_string_shared =
909 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
910 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
911
912 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
913 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
914 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
915
916 fn_t ns_get_public_extern_string =
917 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
918 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
919 fn_t ns_get_public_extern_string_shared =
920 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
921 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
922
923 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
924 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
925 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
926
927 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
928 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
929 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
930 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
931 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
932 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
933
934 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
935 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
936 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
937 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
938 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
939
940 dlclose(handle1);
941 dlclose(handle2);
942}
943
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800944TEST(dlext, ns_anonymous) {
945 static const char* root_lib = "libnstest_root.so";
946 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
947
948 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
949
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800950 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
951 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
952
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800953 ASSERT_TRUE(handle_public != nullptr) << dlerror();
954
955 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
956 << dlerror();
957
958 android_namespace_t* ns = android_create_namespace(
959 "private", nullptr,
960 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800961 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800962
963 ASSERT_TRUE(ns != nullptr) << dlerror();
964
965 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
966
967 android_dlextinfo extinfo;
968 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
969 extinfo.library_namespace = ns;
970
971 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
972 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
973 ASSERT_TRUE(handle != nullptr) << dlerror();
974
975 uintptr_t ns_get_dlopened_string_addr =
976 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
977 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
978 typedef const char* (*fn_t)();
979 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
980
981 std::vector<map_record> maps;
982 Maps::parse_maps(&maps);
983
984 uintptr_t addr_start = 0;
985 uintptr_t addr_end = 0;
986 std::vector<map_record> maps_to_copy;
987
988 for (const auto& rec : maps) {
989 if (rec.pathname == private_library_absolute_path) {
990 if (addr_start == 0) {
991 addr_start = rec.addr_start;
992 }
993 addr_end = rec.addr_end;
994
995 maps_to_copy.push_back(rec);
996 }
997 }
998
999 // some sanity checks..
1000 ASSERT_TRUE(addr_start > 0);
1001 ASSERT_TRUE(addr_end > 0);
1002 ASSERT_EQ(3U, maps_to_copy.size());
1003 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1004 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1005
1006 // copy
1007 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1008 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1009 -1, 0));
1010 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1011
1012 for (const auto& rec : maps_to_copy) {
1013 uintptr_t offset = rec.addr_start - addr_start;
1014 size_t size = rec.addr_end - rec.addr_start;
1015 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1016 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1017 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1018 ASSERT_TRUE(map != MAP_FAILED);
1019 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1020 mprotect(map, size, rec.perms);
1021 }
1022
1023 // call the function copy
1024 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1025 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1026 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1027 ns_get_dlopened_string_anon());
1028
1029 // They should belong to different namespaces (private and anonymous)
1030 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1031 ns_get_dlopened_string_private());
1032
1033 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1034}