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
)();
26 crazy_context_t
* context
= crazy_context_create();
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");
43 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipes
) < 0)
44 Panic("Could not create socket pair: %s", strerror(errno
));
48 Panic("Could not fork test program!");
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");
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");
73 printf("Bar() called, exiting\n");
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");
94 printf("Parent waiting for child\n");
96 // Wait for child to complete.
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
);
107 Panic("Child exited for unknown reason!!\n");
110 printf("Closing libraries\n");
114 crazy_context_destroy(context
);