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.
14 #include <sys/socket.h>
19 #include <crazy_linker.h>
21 #include "test_util.h"
23 typedef void (*FunctionPtr
)();
27 if (!crazy_system_can_share_relro()) {
28 fprintf(stderr
, "WARNING: Test ignored due to broken kernel!!\n");
32 crazy_context_t
* context
= crazy_context_create();
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");
49 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipes
) < 0)
50 Panic("Could not create socket pair: %s", strerror(errno
));
54 Panic("Could not fork test program!");
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");
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");
79 printf("Bar() called, exiting\n");
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");
100 printf("Parent waiting for child\n");
102 // Wait for child to complete.
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
);
113 Panic("Child exited for unknown reason!!\n");
116 printf("Closing libraries\n");
120 crazy_context_destroy(context
);