blob: d28e9ccc91b926784196eb1b51fbf20f8abedd98 [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
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800278 uint32_t* taxicab_number =
279 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700280 ASSERT_DL_NOTNULL(taxicab_number);
281 EXPECT_EQ(1729U, *taxicab_number);
282
Dmitriy Ivanov52393a52015-03-18 22:50:01 -0700283 dlclose(handle);
284}
285
286
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000287TEST_F(DlExtTest, Reserved) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800288 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000289 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());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800302
303 // Check that after dlclose reserved address space is unmapped (and can be reused)
304 dlclose(handle_);
305 handle_ = nullptr;
306
307 void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
308 ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000309}
310
311TEST_F(DlExtTest, ReservedTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800312 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000313 ASSERT_TRUE(start != MAP_FAILED);
314 android_dlextinfo extinfo;
315 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
316 extinfo.reserved_addr = start;
317 extinfo.reserved_size = PAGE_SIZE;
318 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700319 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000320}
321
322TEST_F(DlExtTest, ReservedHint) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800323 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000324 ASSERT_TRUE(start != MAP_FAILED);
325 android_dlextinfo extinfo;
326 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
327 extinfo.reserved_addr = start;
328 extinfo.reserved_size = LIBSIZE;
329 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
330 ASSERT_DL_NOTNULL(handle_);
331 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
332 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700333 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000334 EXPECT_LT(reinterpret_cast<void*>(f),
335 reinterpret_cast<char*>(start) + LIBSIZE);
336 EXPECT_EQ(4, f());
337}
338
339TEST_F(DlExtTest, ReservedHintTooSmall) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800340 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000341 ASSERT_TRUE(start != MAP_FAILED);
342 android_dlextinfo extinfo;
343 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
344 extinfo.reserved_addr = start;
345 extinfo.reserved_size = PAGE_SIZE;
346 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
347 ASSERT_DL_NOTNULL(handle_);
348 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
349 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700350 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
351 (reinterpret_cast<void*>(f) >=
352 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000353 EXPECT_EQ(4, f());
354}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000355
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700356TEST_F(DlExtTest, LoadAtFixedAddress) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800357 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700358 ASSERT_TRUE(start != MAP_FAILED);
359 munmap(start, LIBSIZE);
360
361 android_dlextinfo extinfo;
362 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
363 extinfo.reserved_addr = start;
364
365 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
366 ASSERT_DL_NOTNULL(handle_);
367 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
368 ASSERT_DL_NOTNULL(f);
369 EXPECT_GE(reinterpret_cast<void*>(f), start);
370 EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
371
372 EXPECT_EQ(4, f());
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800373 dlclose(handle_);
374 handle_ = nullptr;
375
376 // Check that dlclose unmapped the file
377 void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
378 ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700379}
380
381TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
382 void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
383 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
384 ASSERT_TRUE(start != MAP_FAILED);
385 munmap(start, LIBSIZE + PAGE_SIZE);
386 void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
387 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
388 ASSERT_TRUE(new_addr != MAP_FAILED);
389
390 android_dlextinfo extinfo;
391 extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
392 extinfo.reserved_addr = start;
393
394 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
395 ASSERT_TRUE(handle_ == nullptr);
396}
397
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100398class DlExtRelroSharingTest : public DlExtTest {
399protected:
400 virtual void SetUp() {
401 DlExtTest::SetUp();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800402 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100403 ASSERT_TRUE(start != MAP_FAILED);
404 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
405 extinfo_.reserved_addr = start;
406 extinfo_.reserved_size = LIBSIZE;
407 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000408 }
409
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100410 virtual void TearDown() {
411 DlExtTest::TearDown();
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100412 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000413
Yabin Cui294d1e22014-12-07 20:43:37 -0800414 void CreateRelroFile(const char* lib, const char* relro_file) {
415 int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100416 ASSERT_NOERROR(relro_fd);
417
418 pid_t pid = fork();
419 if (pid == 0) {
420 // child process
421 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
422 extinfo_.relro_fd = relro_fd;
423 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700424 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100425 fprintf(stderr, "in child: %s\n", dlerror());
426 exit(1);
427 }
428 exit(0);
429 }
430
431 // continuing in parent
432 ASSERT_NOERROR(close(relro_fd));
433 ASSERT_NOERROR(pid);
434 int status;
435 ASSERT_EQ(pid, waitpid(pid, &status, 0));
436 ASSERT_TRUE(WIFEXITED(status));
437 ASSERT_EQ(0, WEXITSTATUS(status));
438
439 // reopen file for reading so it can be used
Yabin Cui294d1e22014-12-07 20:43:37 -0800440 relro_fd = open(relro_file, O_RDONLY);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100441 ASSERT_NOERROR(relro_fd);
442 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
443 extinfo_.relro_fd = relro_fd;
444 }
445
446 void TryUsingRelro(const char* lib) {
447 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
448 ASSERT_DL_NOTNULL(handle_);
449 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
450 ASSERT_DL_NOTNULL(f);
451 EXPECT_EQ(4, f());
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700452
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800453 uint32_t* taxicab_number =
454 reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -0700455 ASSERT_DL_NOTNULL(taxicab_number);
456 EXPECT_EQ(1729U, *taxicab_number);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100457 }
458
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100459 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
460
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100461 android_dlextinfo extinfo_;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100462};
463
464TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800465 TemporaryFile tf; // Use tf to get an unique filename.
466 ASSERT_NOERROR(close(tf.fd));
467
468 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100469 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Yabin Cui294d1e22014-12-07 20:43:37 -0800470
471 // Use destructor of tf to close and unlink the file.
472 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100473}
474
475TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800476 TemporaryFile tf; // // Use tf to get an unique filename.
477 ASSERT_NOERROR(close(tf.fd));
478
479 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100480 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
Yabin Cui294d1e22014-12-07 20:43:37 -0800481
482 // Use destructor of tf to close and unlink the file.
483 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100484}
485
486TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100487 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000488}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100489
490TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700491 if (geteuid() != 0) {
492 GTEST_LOG_(INFO) << "This test must be run as root.\n";
493 return;
494 }
495
Yabin Cui294d1e22014-12-07 20:43:37 -0800496 TemporaryFile tf; // Use tf to get an unique filename.
497 ASSERT_NOERROR(close(tf.fd));
498
499 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
500
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100501 int pipefd[2];
502 ASSERT_NOERROR(pipe(pipefd));
503
504 size_t without_sharing, with_sharing;
505 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
506 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
507
508 // We expect the sharing to save at least 10% of the total PSS. In practice
509 // it saves 40%+ for this test.
510 size_t expected_size = without_sharing - (without_sharing/10);
511 EXPECT_LT(with_sharing, expected_size);
Yabin Cui294d1e22014-12-07 20:43:37 -0800512
513 // Use destructor of tf to close and unlink the file.
514 tf.fd = extinfo_.relro_fd;
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100515}
516
517void getPss(pid_t pid, size_t* pss_out) {
518 pm_kernel_t* kernel;
519 ASSERT_EQ(0, pm_kernel_create(&kernel));
520
521 pm_process_t* process;
522 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
523
524 pm_map_t** maps;
525 size_t num_maps;
526 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
527
528 size_t total_pss = 0;
529 for (size_t i = 0; i < num_maps; i++) {
530 pm_memusage_t usage;
531 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
532 total_pss += usage.pss;
533 }
534 *pss_out = total_pss;
535
536 free(maps);
537 pm_process_destroy(process);
538 pm_kernel_destroy(kernel);
539}
540
541void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
542 size_t* pss_out) {
543 const int CHILDREN = 20;
544
545 // Create children
546 pid_t childpid[CHILDREN];
547 int childpipe[CHILDREN];
548 for (int i=0; i<CHILDREN; ++i) {
549 char read_buf;
550 int child_done_pipe[2], parent_done_pipe[2];
551 ASSERT_NOERROR(pipe(child_done_pipe));
552 ASSERT_NOERROR(pipe(parent_done_pipe));
553
554 pid_t child = fork();
555 if (child == 0) {
556 // close the 'wrong' ends of the pipes in the child
557 close(child_done_pipe[0]);
558 close(parent_done_pipe[1]);
559
560 // open the library
561 void* handle;
562 if (share_relro) {
563 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
564 } else {
565 handle = dlopen(lib, RTLD_NOW);
566 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700567 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100568 fprintf(stderr, "in child: %s\n", dlerror());
569 exit(1);
570 }
571
572 // close write end of child_done_pipe to signal the parent that we're done.
573 close(child_done_pipe[1]);
574
575 // wait for the parent to close parent_done_pipe, then exit
576 read(parent_done_pipe[0], &read_buf, 1);
577 exit(0);
578 }
579
580 ASSERT_NOERROR(child);
581
582 // close the 'wrong' ends of the pipes in the parent
583 close(child_done_pipe[1]);
584 close(parent_done_pipe[0]);
585
586 // wait for the child to be done
587 read(child_done_pipe[0], &read_buf, 1);
588 close(child_done_pipe[0]);
589
590 // save the child's pid and the parent_done_pipe
591 childpid[i] = child;
592 childpipe[i] = parent_done_pipe[1];
593 }
594
595 // Sum the PSS of all the children
596 size_t total_pss = 0;
597 for (int i=0; i<CHILDREN; ++i) {
598 size_t child_pss;
599 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
600 total_pss += child_pss;
601 }
602 *pss_out = total_pss;
603
604 // Close pipes and wait for children to exit
605 for (int i=0; i<CHILDREN; ++i) {
606 ASSERT_NOERROR(close(childpipe[i]));
607 }
608 for (int i=0; i<CHILDREN; ++i) {
609 int status;
610 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
611 ASSERT_TRUE(WIFEXITED(status));
612 ASSERT_EQ(0, WEXITSTATUS(status));
613 }
614}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700615
616// Testing namespaces
617static const char* g_public_lib = "libnstest_public.so";
618
619TEST(dlext, ns_smoke) {
620 static const char* root_lib = "libnstest_root.so";
621 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
622
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800623 ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
624 ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700625 "\"libnstest_public.so\" was not found in the default namespace", dlerror());
626
627 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
628
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800629 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
630 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700631 ASSERT_TRUE(handle_public != nullptr) << dlerror();
632
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800633 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700634
635 // Check that libraries added to public namespace are NODELETE
636 dlclose(handle_public);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800637 handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
638 RTLD_NOW | RTLD_NOLOAD);
639
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700640 ASSERT_TRUE(handle_public != nullptr) << dlerror();
641
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800642 android_namespace_t* ns1 =
643 android_create_namespace("private", nullptr,
644 (lib_path + "/private_namespace_libs").c_str(),
645 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700646 ASSERT_TRUE(ns1 != nullptr) << dlerror();
647
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800648 android_namespace_t* ns2 =
649 android_create_namespace("private_isolated", nullptr,
650 (lib_path + "/private_namespace_libs").c_str(),
651 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700652 ASSERT_TRUE(ns2 != nullptr) << dlerror();
653
654 // This should not have affect search path for default namespace:
655 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
656 void* handle = dlopen(g_public_lib, RTLD_NOW);
657 ASSERT_TRUE(handle != nullptr) << dlerror();
658 dlclose(handle);
659
660 android_dlextinfo extinfo;
661 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
662 extinfo.library_namespace = ns1;
663
664 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
665 ASSERT_TRUE(handle1 != nullptr) << dlerror();
666
667 extinfo.library_namespace = ns2;
668 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
669 ASSERT_TRUE(handle2 != nullptr) << dlerror();
670
671 ASSERT_TRUE(handle1 != handle2);
672
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800673 // dlopen for a public library using an absolute path should work for isolated namespaces
674 extinfo.library_namespace = ns2;
675 handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
676 ASSERT_TRUE(handle != nullptr) << dlerror();
677 ASSERT_TRUE(handle == handle_public);
678
679 dlclose(handle);
680
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700681 typedef const char* (*fn_t)();
682
683 fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
684 ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
685 fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
686 ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
687
688 EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
689 EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
690
691 ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
692
693 fn_t ns_get_private_extern_string1 =
694 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
695 ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
696 fn_t ns_get_private_extern_string2 =
697 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
698 ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
699
700 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
701 EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
702
703 ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
704
705 fn_t ns_get_public_extern_string1 =
706 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
707 ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
708 fn_t ns_get_public_extern_string2 =
709 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
710 ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
711
712 EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
713 ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
714
715 // and now check that dlopen() does the right thing in terms of preserving namespace
716 fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
717 ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
718 fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
719 ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
720
721 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
722 EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
723
724 ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
725
726 dlclose(handle1);
727
728 // Check if handle2 is still alive (and well)
729 ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
730 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
731 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
732 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
733
734 dlclose(handle2);
735}
736
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800737extern "C" void android_set_application_target_sdk_version(uint32_t target);
738
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700739TEST(dlext, ns_isolated) {
740 static const char* root_lib = "libnstest_root_not_isolated.so";
741 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
742
743 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800744 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
745 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700746 ASSERT_TRUE(handle_public != nullptr) << dlerror();
747
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -0800748 android_set_application_target_sdk_version(42U); // something > 23
749
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800750 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700751
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800752 android_namespace_t* ns_not_isolated =
753 android_create_namespace("private", nullptr,
754 (lib_path + "/private_namespace_libs").c_str(),
755 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700756 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
757
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800758 android_namespace_t* ns_isolated =
759 android_create_namespace("private_isolated1", nullptr,
760 (lib_path + "/private_namespace_libs").c_str(),
761 ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700762 ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
763
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800764 android_namespace_t* ns_isolated2 =
765 android_create_namespace("private_isolated2",
766 (lib_path + "/private_namespace_libs").c_str(),
767 nullptr, ANDROID_NAMESPACE_TYPE_ISOLATED, lib_path.c_str());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700768 ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
769
770 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
771 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
772
773 std::string lib_private_external_path =
774 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
775
776 // Load lib_private_external_path to default namespace
777 // (it should remain invisible for the isolated namespaces after this)
778 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
779 ASSERT_TRUE(handle != nullptr) << dlerror();
780
781 android_dlextinfo extinfo;
782 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
783 extinfo.library_namespace = ns_not_isolated;
784
785 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
786 ASSERT_TRUE(handle1 != nullptr) << dlerror();
787
788 extinfo.library_namespace = ns_isolated;
789
790 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
791 ASSERT_TRUE(handle2 == nullptr);
792 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
793
794 // Check dlopen by absolute path
795 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
796 ASSERT_TRUE(handle2 == nullptr);
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800797 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 -0700798
799 extinfo.library_namespace = ns_isolated2;
800
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800801 // this should work because isolation_path for private_isolated2 includes lib_path
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700802 handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800803 ASSERT_TRUE(handle2 != nullptr) << dlerror();
804 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700805
806 // Check dlopen by absolute path
807 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800808 ASSERT_TRUE(handle2 != nullptr) << dlerror();
809 dlclose(handle2);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700810
811 typedef const char* (*fn_t)();
812 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
813 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
814
815 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
816
817 fn_t ns_get_private_extern_string =
818 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
819 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
820
821 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
822
823 fn_t ns_get_public_extern_string =
824 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
825 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
826
827 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
828
829 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
830 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
831
832 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
833
834 dlclose(handle1);
835}
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800836
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800837TEST(dlext, ns_shared) {
838 static const char* root_lib = "libnstest_root_not_isolated.so";
839 static const char* root_lib_isolated = "libnstest_root.so";
840 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
841
842 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
843 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
844 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
845 ASSERT_TRUE(handle_public != nullptr) << dlerror();
846
847 android_set_application_target_sdk_version(42U); // something > 23
848
849 ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
850
851 // preload this library to the default namespace to check if it
852 // is shared later on.
853 void* handle_dlopened =
854 dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
855 ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
856
857 android_namespace_t* ns_not_isolated =
858 android_create_namespace("private", nullptr,
859 (lib_path + "/private_namespace_libs").c_str(),
860 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
861 ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
862
863 android_namespace_t* ns_isolated_shared =
864 android_create_namespace("private_isolated_shared", nullptr,
865 (lib_path + "/private_namespace_libs").c_str(),
866 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
867 nullptr);
868 ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
869
870 ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
871 ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
872
873 std::string lib_private_external_path =
874 lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
875
876 // Load lib_private_external_path to default namespace
877 // (it should remain invisible for the isolated namespaces after this)
878 void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
879 ASSERT_TRUE(handle != nullptr) << dlerror();
880
881 android_dlextinfo extinfo;
882 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
883 extinfo.library_namespace = ns_not_isolated;
884
885 void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
886 ASSERT_TRUE(handle1 != nullptr) << dlerror();
887
888 extinfo.library_namespace = ns_isolated_shared;
889
890 void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
891 ASSERT_TRUE(handle2 == nullptr);
892 ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
893
894 // Check dlopen by absolute path
895 handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
896 ASSERT_TRUE(handle2 == nullptr);
897 ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" is not accessible for the namespace \"private_isolated_shared\"", dlerror());
898
899 // load libnstest_root.so to shared namespace in order to check that everything is different
900 // except shared libnstest_dlopened.so
901
902 handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
903
904 typedef const char* (*fn_t)();
905 fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
906 ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
907 fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
908 ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
909
910 ASSERT_STREQ("This string is local to root library", ns_get_local_string());
911 ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
912 ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
913
914 fn_t ns_get_private_extern_string =
915 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
916 ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
917 fn_t ns_get_private_extern_string_shared =
918 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
919 ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
920
921 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
922 ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
923 ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
924
925 fn_t ns_get_public_extern_string =
926 reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
927 ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
928 fn_t ns_get_public_extern_string_shared =
929 reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
930 ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
931
932 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
933 ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
934 ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
935
936 fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
937 ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
938 fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
939 ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
940 const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
941 ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
942
943 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
944 ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
945 ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
946 ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
947 ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
948
949 dlclose(handle1);
950 dlclose(handle2);
951}
952
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800953TEST(dlext, ns_anonymous) {
954 static const char* root_lib = "libnstest_root.so";
955 std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
956
957 const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
958
Dimitry Ivanov22840aa2015-12-04 18:28:49 -0800959 const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
960 void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
961
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800962 ASSERT_TRUE(handle_public != nullptr) << dlerror();
963
964 ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
965 << dlerror();
966
967 android_namespace_t* ns = android_create_namespace(
968 "private", nullptr,
969 (lib_path + "/private_namespace_libs").c_str(),
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800970 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800971
972 ASSERT_TRUE(ns != nullptr) << dlerror();
973
974 std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
975
976 android_dlextinfo extinfo;
977 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
978 extinfo.library_namespace = ns;
979
980 // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
981 void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
982 ASSERT_TRUE(handle != nullptr) << dlerror();
983
984 uintptr_t ns_get_dlopened_string_addr =
985 reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
986 ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
987 typedef const char* (*fn_t)();
988 fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
989
990 std::vector<map_record> maps;
991 Maps::parse_maps(&maps);
992
993 uintptr_t addr_start = 0;
994 uintptr_t addr_end = 0;
995 std::vector<map_record> maps_to_copy;
996
997 for (const auto& rec : maps) {
998 if (rec.pathname == private_library_absolute_path) {
999 if (addr_start == 0) {
1000 addr_start = rec.addr_start;
1001 }
1002 addr_end = rec.addr_end;
1003
1004 maps_to_copy.push_back(rec);
1005 }
1006 }
1007
1008 // some sanity checks..
1009 ASSERT_TRUE(addr_start > 0);
1010 ASSERT_TRUE(addr_end > 0);
1011 ASSERT_EQ(3U, maps_to_copy.size());
1012 ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
1013 ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
1014
1015 // copy
1016 uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
1017 PROT_NONE, MAP_ANON | MAP_PRIVATE,
1018 -1, 0));
1019 ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
1020
1021 for (const auto& rec : maps_to_copy) {
1022 uintptr_t offset = rec.addr_start - addr_start;
1023 size_t size = rec.addr_end - rec.addr_start;
1024 void* addr = reinterpret_cast<void*>(reserved_addr + offset);
1025 void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
1026 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
1027 ASSERT_TRUE(map != MAP_FAILED);
1028 memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
1029 mprotect(map, size, rec.perms);
1030 }
1031
1032 // call the function copy
1033 uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start;
1034 fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
1035 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1036 ns_get_dlopened_string_anon());
1037
1038 // They should belong to different namespaces (private and anonymous)
1039 ASSERT_STREQ("This string is from private namespace (dlopened library)",
1040 ns_get_dlopened_string_private());
1041
1042 ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
1043}