Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / android_crazy_linker / src / tests / test_shared_relro.cpp
blob08764613b934e380888304d2bfb217c0e23e9294
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 // A crazy linker test to:
6 // - Load a library (libfoo.so) with the linker.
7 // - Find the address of the "Foo" function in it.
8 // - Call the function.
9 // - Close the library.
11 #include <errno.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/socket.h>
16 #include <sys/uio.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
20 #include <crazy_linker.h>
22 #include "test_util.h"
24 typedef void (*FunctionPtr)();
26 int main() {
27 crazy_context_t* context = crazy_context_create();
29 RelroLibrary foo;
31 // Load at fixed address to simplify testing.
32 crazy_context_set_load_address(context, 0x20000000);
33 foo.Init("libfoo_with_relro.so", context);
35 printf("Library loaded\n");
37 int pipes[2];
38 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes) < 0)
39 Panic("Could not create socket pair: %s", strerror(errno));
41 pid_t child = fork();
42 if (child < 0)
43 Panic("Could not fork test program!");
45 if (child == 0) {
46 // In the child.
47 printf("Child waiting for foo relro fd\n");
49 foo.ReceiveRelroInfo(pipes[0]);
50 foo.UseSharedRelro(context);
52 printf("RELRO used in child process\n");
54 CheckRelroMaps(1);
56 FunctionPtr foo_func;
57 if (!crazy_library_find_symbol(
58 foo.library, "Foo", reinterpret_cast<void**>(&foo_func)))
59 Panic("Could not find 'Foo' in library");
61 printf("Calling Foo()\n");
62 (*foo_func)();
64 printf("Foo called, exiting\n");
66 exit(0);
68 } else {
69 // In the parent.
71 printf("Parent enabling foo RELRO sharing\n");
73 foo.EnableSharedRelro(context);
74 foo.SendRelroInfo(pipes[1]);
76 printf("RELRO enabled and sent to child\n");
78 CheckRelroMaps(1);
80 printf("Parent waiting for child\n");
82 // Wait for child to complete.
83 int status;
84 waitpid(child, &status, 0);
86 if (WIFSIGNALED(status))
87 Panic("Child terminated by signal!!\n");
88 else if (WIFEXITED(status)) {
89 int child_status = WEXITSTATUS(status);
90 if (child_status != 0)
91 Panic("Child terminated with status=%d\n", child_status);
92 } else
93 Panic("Child exited for unknown reason!!\n");
96 crazy_context_destroy(context);
97 return 0;