blob: 7585e9b304d2b36bcc7bdce2d0563789d20f3745 [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) \
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070034 ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000035
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__)
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070049#define LIBPATH_PREFIX "%s/nativetest64/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070050#else
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070051#define LIBPATH_PREFIX "%s/nativetest/libdlext_test_fd/"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070052#endif
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070054#define LIBPATH LIBPATH_PREFIX "libdlext_test_fd.so"
55#define LIBZIPPATH LIBPATH_PREFIX "dlext_test.zip"
56
57#define LIBZIP_OFFSET 2*PAGE_SIZE
58
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000059class DlExtTest : public ::testing::Test {
60protected:
61 virtual void SetUp() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070062 handle_ = nullptr;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000063 // verify that we don't have the library loaded already
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070064 void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
65 ASSERT_TRUE(h == nullptr);
66 h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
67 ASSERT_TRUE(h == nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000068 // call dlerror() to swallow the error, and check it was the one we wanted
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070069 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 +000070 }
71
72 virtual void TearDown() {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070073 if (handle_ != nullptr) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000074 ASSERT_DL_ZERO(dlclose(handle_));
75 }
76 }
77
78 void* handle_;
79};
80
81TEST_F(DlExtTest, ExtInfoNull) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070082 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000083 ASSERT_DL_NOTNULL(handle_);
84 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
85 ASSERT_DL_NOTNULL(f);
86 EXPECT_EQ(4, f());
87}
88
89TEST_F(DlExtTest, ExtInfoNoFlags) {
90 android_dlextinfo extinfo;
91 extinfo.flags = 0;
92 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
93 ASSERT_DL_NOTNULL(handle_);
94 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
95 ASSERT_DL_NOTNULL(f);
96 EXPECT_EQ(4, f());
97}
98
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070099TEST_F(DlExtTest, ExtInfoUseFd) {
100 const char* android_data = getenv("ANDROID_DATA");
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700101 ASSERT_TRUE(android_data != nullptr);
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700102 char lib_path[PATH_MAX];
103 snprintf(lib_path, sizeof(lib_path), LIBPATH, android_data);
104
105 android_dlextinfo extinfo;
106 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
107 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
108 ASSERT_TRUE(extinfo.library_fd != -1);
109 handle_ = android_dlopen_ext(lib_path, RTLD_NOW, &extinfo);
110 ASSERT_DL_NOTNULL(handle_);
111 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
112 ASSERT_DL_NOTNULL(f);
113 EXPECT_EQ(4, f());
114}
115
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700116TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
117 const char* android_data = getenv("ANDROID_DATA");
118 ASSERT_TRUE(android_data != nullptr);
119
120 char lib_path[PATH_MAX];
121 snprintf(lib_path, sizeof(lib_path), LIBZIPPATH, android_data);
122
123 android_dlextinfo extinfo;
124 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_OFFSET;
125 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
126 extinfo.library_offset = LIBZIP_OFFSET;
127
128 handle_ = android_dlopen_ext(lib_path, RTLD_NOW, &extinfo);
129 ASSERT_DL_NOTNULL(handle_);
130
131 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
132 ASSERT_DL_NOTNULL(f);
133 EXPECT_EQ(4, f());
134}
135
136TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
137 const char* android_data = getenv("ANDROID_DATA");
138 ASSERT_TRUE(android_data != nullptr);
139
140 char lib_path[PATH_MAX];
141 snprintf(lib_path, sizeof(lib_path), LIBZIPPATH, android_data);
142
143 android_dlextinfo extinfo;
144 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_OFFSET;
145 extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path, O_RDONLY | O_CLOEXEC));
146 extinfo.library_offset = 17;
147
148 handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
149 ASSERT_TRUE(handle_ == nullptr);
150 ASSERT_STREQ("dlopen failed: file offset for the library libname_placeholder is not page-aligned: 17", dlerror());
151}
152
153TEST_F(DlExtTest, ExtInfoUseOffsetWihtoutFd) {
154 android_dlextinfo extinfo;
155 extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_OFFSET;
156 extinfo.library_offset = LIBZIP_OFFSET;
157
158 handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
159 ASSERT_TRUE(handle_ == nullptr);
160 ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror());
161}
162
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000163TEST_F(DlExtTest, Reserved) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700164 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000165 -1, 0);
166 ASSERT_TRUE(start != MAP_FAILED);
167 android_dlextinfo extinfo;
168 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
169 extinfo.reserved_addr = start;
170 extinfo.reserved_size = LIBSIZE;
171 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
172 ASSERT_DL_NOTNULL(handle_);
173 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
174 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700175 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000176 EXPECT_LT(reinterpret_cast<void*>(f),
177 reinterpret_cast<char*>(start) + LIBSIZE);
178 EXPECT_EQ(4, f());
179}
180
181TEST_F(DlExtTest, ReservedTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700182 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000183 -1, 0);
184 ASSERT_TRUE(start != MAP_FAILED);
185 android_dlextinfo extinfo;
186 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
187 extinfo.reserved_addr = start;
188 extinfo.reserved_size = PAGE_SIZE;
189 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700190 EXPECT_EQ(nullptr, handle_);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000191}
192
193TEST_F(DlExtTest, ReservedHint) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700194 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000195 -1, 0);
196 ASSERT_TRUE(start != MAP_FAILED);
197 android_dlextinfo extinfo;
198 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
199 extinfo.reserved_addr = start;
200 extinfo.reserved_size = LIBSIZE;
201 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
202 ASSERT_DL_NOTNULL(handle_);
203 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
204 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700205 EXPECT_GE(reinterpret_cast<void*>(f), start);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000206 EXPECT_LT(reinterpret_cast<void*>(f),
207 reinterpret_cast<char*>(start) + LIBSIZE);
208 EXPECT_EQ(4, f());
209}
210
211TEST_F(DlExtTest, ReservedHintTooSmall) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700212 void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000213 -1, 0);
214 ASSERT_TRUE(start != MAP_FAILED);
215 android_dlextinfo extinfo;
216 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
217 extinfo.reserved_addr = start;
218 extinfo.reserved_size = PAGE_SIZE;
219 handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
220 ASSERT_DL_NOTNULL(handle_);
221 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
222 ASSERT_DL_NOTNULL(f);
Chih-Hung Hsieha2c6ae62014-08-27 13:45:37 -0700223 EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
224 (reinterpret_cast<void*>(f) >=
225 reinterpret_cast<char*>(start) + PAGE_SIZE));
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000226 EXPECT_EQ(4, f());
227}
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000228
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100229class DlExtRelroSharingTest : public DlExtTest {
230protected:
231 virtual void SetUp() {
232 DlExtTest::SetUp();
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700233 void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100234 -1, 0);
235 ASSERT_TRUE(start != MAP_FAILED);
236 extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
237 extinfo_.reserved_addr = start;
238 extinfo_.reserved_size = LIBSIZE;
239 extinfo_.relro_fd = -1;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000240
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100241 const char* android_data = getenv("ANDROID_DATA");
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700242 ASSERT_TRUE(android_data != nullptr);
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100243 snprintf(relro_file_, sizeof(relro_file_), "%s/local/tmp/libdlext_test.relro", android_data);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000244 }
245
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100246 virtual void TearDown() {
247 DlExtTest::TearDown();
248 if (extinfo_.relro_fd != -1) {
249 ASSERT_NOERROR(close(extinfo_.relro_fd));
250 }
251 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000252
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100253 void CreateRelroFile(const char* lib) {
254 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
255 ASSERT_NOERROR(relro_fd);
256
257 pid_t pid = fork();
258 if (pid == 0) {
259 // child process
260 extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
261 extinfo_.relro_fd = relro_fd;
262 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700263 if (handle == nullptr) {
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100264 fprintf(stderr, "in child: %s\n", dlerror());
265 exit(1);
266 }
267 exit(0);
268 }
269
270 // continuing in parent
271 ASSERT_NOERROR(close(relro_fd));
272 ASSERT_NOERROR(pid);
273 int status;
274 ASSERT_EQ(pid, waitpid(pid, &status, 0));
275 ASSERT_TRUE(WIFEXITED(status));
276 ASSERT_EQ(0, WEXITSTATUS(status));
277
278 // reopen file for reading so it can be used
279 relro_fd = open(relro_file_, O_RDONLY);
280 ASSERT_NOERROR(relro_fd);
281 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
282 extinfo_.relro_fd = relro_fd;
283 }
284
285 void TryUsingRelro(const char* lib) {
286 handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
287 ASSERT_DL_NOTNULL(handle_);
288 fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
289 ASSERT_DL_NOTNULL(f);
290 EXPECT_EQ(4, f());
291 }
292
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100293 void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
294
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100295 android_dlextinfo extinfo_;
296 char relro_file_[PATH_MAX];
297};
298
299TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
300 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
301 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
302}
303
304TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
305 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO));
306 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
307}
308
309TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
310 int relro_fd = open(relro_file_, O_CREAT | O_RDWR | O_TRUNC, 0644);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000311 ASSERT_NOERROR(relro_fd);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000312 ASSERT_NOERROR(close(relro_fd));
313
Torne (Richard Coles)26ec9672014-04-30 15:48:40 +0100314 ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000315}
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100316
317TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
Dan Albert69fb9f32014-09-03 11:30:21 -0700318 if (geteuid() != 0) {
319 GTEST_LOG_(INFO) << "This test must be run as root.\n";
320 return;
321 }
322
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100323 ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
324 int relro_fd = open(relro_file_, O_RDONLY);
325 ASSERT_NOERROR(relro_fd);
326 extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
327 extinfo_.relro_fd = relro_fd;
328 int pipefd[2];
329 ASSERT_NOERROR(pipe(pipefd));
330
331 size_t without_sharing, with_sharing;
332 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
333 ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
334
335 // We expect the sharing to save at least 10% of the total PSS. In practice
336 // it saves 40%+ for this test.
337 size_t expected_size = without_sharing - (without_sharing/10);
338 EXPECT_LT(with_sharing, expected_size);
339}
340
341void getPss(pid_t pid, size_t* pss_out) {
342 pm_kernel_t* kernel;
343 ASSERT_EQ(0, pm_kernel_create(&kernel));
344
345 pm_process_t* process;
346 ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
347
348 pm_map_t** maps;
349 size_t num_maps;
350 ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
351
352 size_t total_pss = 0;
353 for (size_t i = 0; i < num_maps; i++) {
354 pm_memusage_t usage;
355 ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
356 total_pss += usage.pss;
357 }
358 *pss_out = total_pss;
359
360 free(maps);
361 pm_process_destroy(process);
362 pm_kernel_destroy(kernel);
363}
364
365void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
366 size_t* pss_out) {
367 const int CHILDREN = 20;
368
369 // Create children
370 pid_t childpid[CHILDREN];
371 int childpipe[CHILDREN];
372 for (int i=0; i<CHILDREN; ++i) {
373 char read_buf;
374 int child_done_pipe[2], parent_done_pipe[2];
375 ASSERT_NOERROR(pipe(child_done_pipe));
376 ASSERT_NOERROR(pipe(parent_done_pipe));
377
378 pid_t child = fork();
379 if (child == 0) {
380 // close the 'wrong' ends of the pipes in the child
381 close(child_done_pipe[0]);
382 close(parent_done_pipe[1]);
383
384 // open the library
385 void* handle;
386 if (share_relro) {
387 handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
388 } else {
389 handle = dlopen(lib, RTLD_NOW);
390 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700391 if (handle == nullptr) {
Torne (Richard Coles)26052612014-05-02 14:57:42 +0100392 fprintf(stderr, "in child: %s\n", dlerror());
393 exit(1);
394 }
395
396 // close write end of child_done_pipe to signal the parent that we're done.
397 close(child_done_pipe[1]);
398
399 // wait for the parent to close parent_done_pipe, then exit
400 read(parent_done_pipe[0], &read_buf, 1);
401 exit(0);
402 }
403
404 ASSERT_NOERROR(child);
405
406 // close the 'wrong' ends of the pipes in the parent
407 close(child_done_pipe[1]);
408 close(parent_done_pipe[0]);
409
410 // wait for the child to be done
411 read(child_done_pipe[0], &read_buf, 1);
412 close(child_done_pipe[0]);
413
414 // save the child's pid and the parent_done_pipe
415 childpid[i] = child;
416 childpipe[i] = parent_done_pipe[1];
417 }
418
419 // Sum the PSS of all the children
420 size_t total_pss = 0;
421 for (int i=0; i<CHILDREN; ++i) {
422 size_t child_pss;
423 ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
424 total_pss += child_pss;
425 }
426 *pss_out = total_pss;
427
428 // Close pipes and wait for children to exit
429 for (int i=0; i<CHILDREN; ++i) {
430 ASSERT_NOERROR(close(childpipe[i]));
431 }
432 for (int i=0; i<CHILDREN; ++i) {
433 int status;
434 ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
435 ASSERT_TRUE(WIFEXITED(status));
436 ASSERT_EQ(0, WEXITSTATUS(status));
437 }
438}