Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / android_crazy_linker / src / tests / test_two_shared_relros.cpp
blob388dfc743a525b1223fb7993c467781664233d23
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Same as test_relro_sharing.cpp, but uses two libraries at the same
6 // time (libfoo_with_relro.so and libbar_with_relro.so), each one of
7 // them gets its own shared RELRO.
9 #include <errno.h>
10 #include <pthread.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/uio.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
19 #include <crazy_linker.h>
21 #include "test_util.h"
23 typedef void (*FunctionPtr)();
25 int main() {
26 crazy_context_t* context = crazy_context_create();
28 RelroLibrary foo;
29 RelroLibrary bar;
31 crazy_context_add_search_path_for_address(context, (void*)&main);
33 // Load libfoo_with_relro.so
34 crazy_context_set_load_address(context, 0x20000000);
35 foo.Init("libfoo_with_relro.so", context);
37 crazy_context_set_load_address(context, 0x20800000);
38 bar.Init("libbar_with_relro.so", context);
40 printf("Libraries loaded\n");
42 int pipes[2];
43 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes) < 0)
44 Panic("Could not create socket pair: %s", strerror(errno));
46 pid_t child = fork();
47 if (child < 0)
48 Panic("Could not fork test program!");
50 if (child == 0) {
51 // In the child.
52 printf("Child waiting for foo relro fd\n");
54 foo.ReceiveRelroInfo(pipes[0]);
55 foo.UseSharedRelro(context);
57 printf("Child waiting for bar relro fd\n");
58 bar.ReceiveRelroInfo(pipes[0]);
59 bar.UseSharedRelro(context);
61 printf("RELROs used in child process\n");
63 CheckRelroMaps(2);
65 FunctionPtr bar_func;
66 if (!crazy_library_find_symbol(
67 bar.library, "Bar", reinterpret_cast<void**>(&bar_func)))
68 Panic("Could not find 'Bar' in library");
70 printf("Calling Bar()\n");
71 (*bar_func)();
73 printf("Bar() called, exiting\n");
75 exit(0);
77 } else {
78 // In the parent.
80 printf("Parent enabling foo RELRO sharing\n");
82 foo.EnableSharedRelro(context);
83 foo.SendRelroInfo(pipes[1]);
85 printf("Parent enabling bar RELRO sharing\n");
87 bar.EnableSharedRelro(context);
88 bar.SendRelroInfo(pipes[1]);
90 printf("RELROs enabled and sent to child\n");
92 CheckRelroMaps(2);
94 printf("Parent waiting for child\n");
96 // Wait for child to complete.
97 int status;
98 waitpid(child, &status, 0);
100 if (WIFSIGNALED(status))
101 Panic("Child terminated by signal!!\n");
102 else if (WIFEXITED(status)) {
103 int child_status = WEXITSTATUS(status);
104 if (child_status != 0)
105 Panic("Child terminated with status=%d\n", child_status);
106 } else
107 Panic("Child exited for unknown reason!!\n");
110 printf("Closing libraries\n");
111 bar.Close();
112 foo.Close();
114 crazy_context_destroy(context);
115 return 0;