blob: 7bd59c84c646f5e7b2ffa5796ac1bf962a9622dd [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>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000020#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000025#include <android/dlext.h>
26#include <sys/mman.h>
Torne (Richard Coles)26052612014-05-02 14:57:42 +010027#include <sys/types.h>
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000028#include <sys/wait.h>
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000029
Torne (Richard Coles)26052612014-05-02 14:57:42 +010030#include <pagemap/pagemap.h>
31
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000032
33#define ASSERT_DL_NOTNULL(ptr) \
34 ASSERT_TRUE(ptr != NULL) << "dlerror: " << dlerror()
35
36#define ASSERT_DL_ZERO(i) \
37 ASSERT_EQ(0, i) << "dlerror: " << dlerror()
38
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000039#define ASSERT_NOERROR(i) \
40 ASSERT_NE(-1, i) << "errno: " << strerror(errno)
41
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000042
43typedef int (*fn)(void);
44#define LIBNAME "libdlext_test.so"
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +010045#define LIBNAME_NORELRO "libdlext_test_norelro.so"
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000046#define LIBSIZE 1024*1024 // how much address space to reserve for it
47
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070048#if defined(__LP64__)
49#define LIBPATH "%s/nativetest64/libdlext_test_fd/libdlext_test_fd.so"
50#else
51#define LIBPATH "%s/nativetest/libdlext_test_fd/libdlext_test_fd.so"
52#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053
54class DlExtTest : public ::testing::Test {
55protected:
56 virtual void SetUp() {
57 handle_ = NULL;
58 // verify that we don't have the library loaded already
59 ASSERT_EQ(NULL, dlsym(RTLD_DEFAULT, "getRandomNumber"));
60 // call dlerror() to swallow the error, and check it was the one we wanted
61 ASSERT_STREQ("undefined symbol: getRandomNumber", dlerror());
62 }
63
64 virtual void TearDown() {
65 if (handle_ != NULL) {
66 ASSERT_DL_ZERO(dlclose(handle_));
67 }
68 }
69
70 void* handle_;
71};
72
73TEST_F(DlExtTest, ExtInfoNull) {
74 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, NULL);
75 ASSERT_DL_NOTNULL(handle_);
76 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
77 ASSERT_DL_NOTNULL(f);
78 EXPECT_EQ(4, f());
79}
80
81TEST_F(DlExtTest, ExtInfoNoFlags) {
82 android_dlextinfo extinfo;
83 extinfo.flags = 0;
84 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
85 ASSERT_DL_NOTNULL(handle_);
86 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
87 ASSERT_DL_NOTNULL(f);
88 EXPECT_EQ(4, f());
89}
90
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070091TEST_F(DlExtTest, ExtInfoUseFd) {
92 const char* android_data = getenv("ANDROID_DATA");
93 ASSERT_TRUE(android_data != NULL);
94 char lib_path[PATH_MAX];
95 snprintf(lib_path, sizeof(lib_path), LIBPATH, android_data);
96
97 android_dlextinfo extinfo;
98 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
99 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
100 ASSERT_TRUE(extinfo.library_fd != -1);
101 handle_ = android_dlopen_ext(lib_path, RTLD_NOW, &extinfo);
102 ASSERT_DL_NOTNULL(handle_);
103 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
104 ASSERT_DL_NOTNULL(f);
105 EXPECT_EQ(4, f());
106}
107
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000108TEST_F(DlExtTest, Reserved) {
109 void* start = mmap(NULL, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
110 -1, 0);
111 ASSERT_TRUE(start != MAP_FAILED);
112 android_dlextinfo extinfo;
113 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
114 extinfo.reserved_addr = start;
115 extinfo.reserved_size = LIBSIZE;
116 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
117 ASSERT_DL_NOTNULL(handle_);
118 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
119 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700120 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000121 EXPECT_LT(reinterpret_cast<void*>(f),
122 reinterpret_cast<char*>(start) + LIBSIZE);
123 EXPECT_EQ(4, f());
124}
125
126TEST_F(DlExtTest, ReservedTooSmall) {
127 void* start = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
128 -1, 0);
129 ASSERT_TRUE(start != MAP_FAILED);
130 android_dlextinfo extinfo;
131 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
132 extinfo.reserved_addr = start;
133 extinfo.reserved_size = PAGE_SIZE;
134 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
135 EXPECT_EQ(NULL, handle_);
136}
137
138TEST_F(DlExtTest, ReservedHint) {
139 void* start = mmap(NULL, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
140 -1, 0);
141 ASSERT_TRUE(start != MAP_FAILED);
142 android_dlextinfo extinfo;
143 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
144 extinfo.reserved_addr = start;
145 extinfo.reserved_size = LIBSIZE;
146 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
147 ASSERT_DL_NOTNULL(handle_);
148 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
149 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700150 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000151 EXPECT_LT(reinterpret_cast<void*>(f),
152 reinterpret_cast<char*>(start) + LIBSIZE);
153 EXPECT_EQ(4, f());
154}
155
156TEST_F(DlExtTest, ReservedHintTooSmall) {
157 void* start = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
158 -1, 0);
159 ASSERT_TRUE(start != MAP_FAILED);
160 android_dlextinfo extinfo;
161 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
162 extinfo.reserved_addr = start;
163 extinfo.reserved_size = PAGE_SIZE;
164 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
165 ASSERT_DL_NOTNULL(handle_);
166 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
167 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700168 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
169 (reinterpret_cast<void*>(f) >=
170 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000171 EXPECT_EQ(4, f());
172}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000173
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100174class DlExtRelroSharingTest : public DlExtTest {
175protected:
176 virtual void SetUp() {
177 DlExtTest::SetUp();
178 void* start = mmap(NULL, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
179 -1, 0);
180 ASSERT_TRUE(start != MAP_FAILED);
181 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
182 extinfo_.reserved_addr = start;
183 extinfo_.reserved_size = LIBSIZE;
184 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000185
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100186 const char* android_data = getenv("ANDROID_DATA");
187 ASSERT_TRUE(android_data != NULL);
188 snprintf(relro_file_, sizeof(relro_file_), "%s/local/tmp/libdlext_test.relro", android_data);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000189 }
190
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100191 virtual void TearDown() {
192 DlExtTest::TearDown();
193 if (extinfo_.relro_fd != -1) {
194 ASSERT_NOERROR(close(extinfo_.relro_fd));
195 }
196 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000197
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100198 void CreateRelroFile(const char* lib) {
199 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
200 ASSERT_NOERROR(relro_fd);
201
202 pid_t pid = fork();
203 if (pid == 0) {
204 // child process
205 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
206 extinfo_.relro_fd = relro_fd;
207 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
208 if (handle == NULL) {
209 fprintf(stderr, "in child: %s\n", dlerror());
210 exit(1);
211 }
212 exit(0);
213 }
214
215 // continuing in parent
216 ASSERT_NOERROR(close(relro_fd));
217 ASSERT_NOERROR(pid);
218 int status;
219 ASSERT_EQ(pid, waitpid(pid, &status, 0));
220 ASSERT_TRUE(WIFEXITED(status));
221 ASSERT_EQ(0, WEXITSTATUS(status));
222
223 // reopen file for reading so it can be used
224 relro_fd = open(relro_file_, O_RDONLY);
225 ASSERT_NOERROR(relro_fd);
226 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
227 extinfo_.relro_fd = relro_fd;
228 }
229
230 void TryUsingRelro(const char* lib) {
231 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
232 ASSERT_DL_NOTNULL(handle_);
233 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
234 ASSERT_DL_NOTNULL(f);
235 EXPECT_EQ(4, f());
236 }
237
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100238 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
239
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100240 android_dlextinfo extinfo_;
241 char relro_file_[PATH_MAX];
242};
243
244TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
245 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
246 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
247}
248
249TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
250 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO));
251 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
252}
253
254TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
255 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000256 ASSERT_NOERROR(relro_fd);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000257 ASSERT_NOERROR(close(relro_fd));
258
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100259 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000260}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100261
262TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700263 if (geteuid() != 0) {
264 GTEST_LOG_(INFO) << "This test must be run as root.\n";
265 return;
266 }
267
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100268 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
269 int relro_fd = open(relro_file_, O_RDONLY);
270 ASSERT_NOERROR(relro_fd);
271 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
272 extinfo_.relro_fd = relro_fd;
273 int pipefd[2];
274 ASSERT_NOERROR(pipe(pipefd));
275
276 size_t without_sharing, with_sharing;
277 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
278 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
279
280 // We expect the sharing to save at least 10% of the total PSS. In practice
281 // it saves 40%+ for this test.
282 size_t expected_size = without_sharing - (without_sharing/10);
283 EXPECT_LT(with_sharing, expected_size);
284}
285
286void getPss(pid_t pid, size_t* pss_out) {
287 pm_kernel_t* kernel;
288 ASSERT_EQ(0, pm_kernel_create(&kernel));
289
290 pm_process_t* process;
291 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
292
293 pm_map_t** maps;
294 size_t num_maps;
295 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
296
297 size_t total_pss = 0;
298 for (size_t i = 0; i < num_maps; i++) {
299 pm_memusage_t usage;
300 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
301 total_pss += usage.pss;
302 }
303 *pss_out = total_pss;
304
305 free(maps);
306 pm_process_destroy(process);
307 pm_kernel_destroy(kernel);
308}
309
310void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
311 size_t* pss_out) {
312 const int CHILDREN = 20;
313
314 // Create children
315 pid_t childpid[CHILDREN];
316 int childpipe[CHILDREN];
317 for (int i=0; i<CHILDREN; ++i) {
318 char read_buf;
319 int child_done_pipe[2], parent_done_pipe[2];
320 ASSERT_NOERROR(pipe(child_done_pipe));
321 ASSERT_NOERROR(pipe(parent_done_pipe));
322
323 pid_t child = fork();
324 if (child == 0) {
325 // close the 'wrong' ends of the pipes in the child
326 close(child_done_pipe[0]);
327 close(parent_done_pipe[1]);
328
329 // open the library
330 void* handle;
331 if (share_relro) {
332 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
333 } else {
334 handle = dlopen(lib, RTLD_NOW);
335 }
336 if (handle == NULL) {
337 fprintf(stderr, "in child: %s\n", dlerror());
338 exit(1);
339 }
340
341 // close write end of child_done_pipe to signal the parent that we're done.
342 close(child_done_pipe[1]);
343
344 // wait for the parent to close parent_done_pipe, then exit
345 read(parent_done_pipe[0], &read_buf, 1);
346 exit(0);
347 }
348
349 ASSERT_NOERROR(child);
350
351 // close the 'wrong' ends of the pipes in the parent
352 close(child_done_pipe[1]);
353 close(parent_done_pipe[0]);
354
355 // wait for the child to be done
356 read(child_done_pipe[0], &read_buf, 1);
357 close(child_done_pipe[0]);
358
359 // save the child's pid and the parent_done_pipe
360 childpid[i] = child;
361 childpipe[i] = parent_done_pipe[1];
362 }
363
364 // Sum the PSS of all the children
365 size_t total_pss = 0;
366 for (int i=0; i<CHILDREN; ++i) {
367 size_t child_pss;
368 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
369 total_pss += child_pss;
370 }
371 *pss_out = total_pss;
372
373 // Close pipes and wait for children to exit
374 for (int i=0; i<CHILDREN; ++i) {
375 ASSERT_NOERROR(close(childpipe[i]));
376 }
377 for (int i=0; i<CHILDREN; ++i) {
378 int status;
379 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
380 ASSERT_TRUE(WIFEXITED(status));
381 ASSERT_EQ(0, WEXITSTATUS(status));
382 }
383}