* testsuite/relro_test.cc: Include <cstdio>, <cstdlib>, and
[external/binutils.git] / gold / testsuite / relro_test.cc
1 // relro_test.cc -- test -z relro for gold
2
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include <cassert>
24 #include <csignal>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <exception>
28 #include <stdint.h>
29 #include <unistd.h>
30
31 // This code is put into a shared library linked with -z relro.
32
33 // i1 and i2 are not relro variables.
34 int i1 = 1;
35 static int i2 = 2;
36
37 // P1 is a global relro variable.
38 int* const p1 = &i1;
39
40 // P2 is a local relro variable.
41 int* const p2 = &i2;
42
43 // Test symbol addresses.
44
45 bool
46 t1()
47 {
48   void* i1addr = static_cast<void*>(&i1);
49   void* i2addr = static_cast<void*>(&i2);
50   const void* p1addr = static_cast<const void*>(&p1);
51   const void* p2addr = static_cast<const void*>(&p2);
52
53   // The relro variables should precede the non-relro variables in the
54   // memory image.
55   assert(i1addr > p1addr);
56   assert(i1addr > p2addr);
57   assert(i2addr > p1addr);
58   assert(i2addr > p2addr);
59
60   // The relro variables should not be on the same page as the
61   // non-relro variables.
62   const size_t page_size = getpagesize();
63   uintptr_t i1page = reinterpret_cast<uintptr_t>(i1addr) & ~ (page_size - 1);
64   uintptr_t i2page = reinterpret_cast<uintptr_t>(i2addr) & ~ (page_size - 1);
65   uintptr_t p1page = reinterpret_cast<uintptr_t>(p1addr) & ~ (page_size - 1);
66   uintptr_t p2page = reinterpret_cast<uintptr_t>(p2addr) & ~ (page_size - 1);
67   assert(i1page != p1page);
68   assert(i1page != p2page);
69   assert(i2page != p1page);
70   assert(i2page != p2page);
71
72   return true;
73 }
74
75 // Tell terminate handler that we are throwing from a signal handler.
76
77 static bool throwing;
78
79 // A signal handler for SIGSEGV.
80
81 extern "C"
82 void
83 sigsegv_handler(int)
84 {
85   throwing = true;
86   throw 0;
87 }
88
89 // The original terminate handler.
90
91 std::terminate_handler orig_terminate;
92
93 // Throwing an exception out of a signal handler doesn't always work
94 // reliably.  When that happens the program will call terminate.  We
95 // set a terminate handler to indicate that the test probably passed.
96
97 void
98 terminate_handler()
99 {
100   if (!throwing)
101     {
102       orig_terminate();
103       ::exit(EXIT_FAILURE);
104     }
105   fprintf(stderr,
106           "relro_test: terminate called due to failure to throw through signal handler\n");
107   fprintf(stderr, "relro_test: assuming test succeeded\n");
108   ::exit(EXIT_SUCCESS);
109 }
110
111 // Use a separate function to throw the exception, so that we don't
112 // need to use -fnon-call-exceptions.
113
114 void f2() __attribute__ ((noinline));
115 void
116 f2()
117 {
118   int** pp1 = const_cast<int**>(&p1);
119   *pp1 = &i2;
120
121   // We shouldn't get here--the assignment to *pp1 should write to
122   // memory which the dynamic linker marked as read-only, giving us a
123   // SIGSEGV, causing sigsegv_handler to be invoked, to throw past us.
124   assert(0);
125 }
126
127 // Changing a relro variable should give us a SIGSEGV.
128
129 bool
130 t2()
131 {
132   signal(SIGSEGV, sigsegv_handler);
133   orig_terminate = std::set_terminate(terminate_handler);
134
135   try
136     {
137       f2();
138       return false;
139     }
140   catch (int i)
141     {
142       assert(i == 0);
143       return true;
144     }
145 }