ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / third_party / android_crazy_linker / src / tests / test_two_shared_relros.cpp
blobeba0f014d8cf1e72e0003d664f701be6694dbb55
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() {
27 if (!crazy_system_can_share_relro()) {
28 fprintf(stderr, "WARNING: Test ignored due to broken kernel!!\n");
29 return 0;
32 crazy_context_t* context = crazy_context_create();
34 RelroLibrary foo;
35 RelroLibrary bar;
37 crazy_context_add_search_path_for_address(context, (void*)&main);
39 // Load libfoo_with_relro.so
40 crazy_context_set_load_address(context, 0x20000000);
41 foo.Init("libfoo_with_relro.so", context);
43 crazy_context_set_load_address(context, 0x20800000);
44 bar.Init("libbar_with_relro.so", context);
46 printf("Libraries loaded\n");
48 int pipes[2];
49 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes) < 0)
50 Panic("Could not create socket pair: %s", strerror(errno));
52 pid_t child = fork();
53 if (child < 0)
54 Panic("Could not fork test program!");
56 if (child == 0) {
57 // In the child.
58 printf("Child waiting for foo relro fd\n");
60 foo.ReceiveRelroInfo(pipes[0]);
61 foo.UseSharedRelro(context);
63 printf("Child waiting for bar relro fd\n");
64 bar.ReceiveRelroInfo(pipes[0]);
65 bar.UseSharedRelro(context);
67 printf("RELROs used in child process\n");
69 CheckRelroMaps(2);
71 FunctionPtr bar_func;
72 if (!crazy_library_find_symbol(
73 bar.library, "Bar", reinterpret_cast<void**>(&bar_func)))
74 Panic("Could not find 'Bar' in library");
76 printf("Calling Bar()\n");
77 (*bar_func)();
79 printf("Bar() called, exiting\n");
81 exit(0);
83 } else {
84 // In the parent.
86 printf("Parent enabling foo RELRO sharing\n");
88 foo.EnableSharedRelro(context);
89 foo.SendRelroInfo(pipes[1]);
91 printf("Parent enabling bar RELRO sharing\n");
93 bar.EnableSharedRelro(context);
94 bar.SendRelroInfo(pipes[1]);
96 printf("RELROs enabled and sent to child\n");
98 CheckRelroMaps(2);
100 printf("Parent waiting for child\n");
102 // Wait for child to complete.
103 int status;
104 waitpid(child, &status, 0);
106 if (WIFSIGNALED(status))
107 Panic("Child terminated by signal!!\n");
108 else if (WIFEXITED(status)) {
109 int child_status = WEXITSTATUS(status);
110 if (child_status != 0)
111 Panic("Child terminated with status=%d\n", child_status);
112 } else
113 Panic("Child exited for unknown reason!!\n");
116 printf("Closing libraries\n");
117 bar.Close();
118 foo.Close();
120 crazy_context_destroy(context);
121 return 0;