nptl: Invert the mmap/mprotect logic on allocated stacks (BZ#18988)
[glibc.git] / dlfcn / tststatic4.c
blob55ce52ba85bed3bf82d74c265db242d19fc82201
1 /* Global object symbol access tests with a static executable (BZ #15022).
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <stddef.h>
21 #include <stdio.h>
23 #define MAGIC0 0
24 #define MAGIC1 0x5500ffaa
25 #define MAGIC2 0xaaff0055
26 #define MAGIC3 0xff55aa00
28 /* Check the ability to access the global symbol object and then
29 global-scope symbol access consistency via different mappings
30 requested from a static executable. */
31 static int
32 do_test (void)
34 unsigned int (*initial_getfoo) (void);
35 void (*initial_setfoo) (unsigned int);
36 unsigned int (*global_getfoo) (void);
37 void (*global_setfoo) (unsigned int);
38 unsigned int (*local_getfoo) (void);
39 void (*local_setfoo) (unsigned int);
40 unsigned int *initial_foop;
41 unsigned int *global_foop;
42 unsigned int *local_foop;
43 void *initial_handle;
44 void *global_handle;
45 void *local_handle;
46 unsigned int foo;
48 /* Try to map self. */
49 initial_handle = dlopen (NULL, RTLD_LAZY | RTLD_GLOBAL);
50 if (initial_handle == NULL)
52 printf ("dlopen [initial] (NULL): %s\n", dlerror ());
53 return 1;
56 /* Make sure symbol lookups fail gracefully. */
57 initial_foop = dlsym (initial_handle, "foo");
58 if (initial_foop != NULL)
60 printf ("dlsym [initial] (foo): got %p, expected NULL\n", initial_foop);
61 return 1;
64 initial_getfoo = dlsym (initial_handle, "getfoo");
65 if (initial_getfoo != NULL)
67 printf ("dlsym [initial] (getfoo): got %p, expected NULL\n",
68 initial_getfoo);
69 return 1;
72 initial_setfoo = dlsym (initial_handle, "setfoo");
73 if (initial_setfoo != NULL)
75 printf ("dlsym [initial] (setfoo): got %p, expected NULL\n",
76 initial_setfoo);
77 return 1;
80 /* Try to map a module into the global scope. */
81 global_handle = dlopen ("modstatic3.so", RTLD_LAZY | RTLD_GLOBAL);
82 if (global_handle == NULL)
84 printf ("dlopen [global] (modstatic3.so): %s\n", dlerror ());
85 return 1;
88 /* Get at its symbols. */
89 global_foop = dlsym (global_handle, "foo");
90 if (global_foop == NULL)
92 printf ("dlsym [global] (foo): %s\n", dlerror ());
93 return 1;
96 global_getfoo = dlsym (global_handle, "getfoo");
97 if (global_getfoo == NULL)
99 printf ("dlsym [global] (getfoo): %s\n", dlerror ());
100 return 1;
103 global_setfoo = dlsym (global_handle, "setfoo");
104 if (global_setfoo == NULL)
106 printf ("dlsym [global] (setfoo): %s\n", dlerror ());
107 return 1;
110 /* Try to map self again now. */
111 local_handle = dlopen (NULL, RTLD_LAZY | RTLD_LOCAL);
112 if (local_handle == NULL)
114 printf ("dlopen [local] (NULL): %s\n", dlerror ());
115 return 1;
118 /* Make sure we can get at the previously loaded module's symbols
119 via this handle too. */
120 local_foop = dlsym (local_handle, "foo");
121 if (local_foop == NULL)
123 printf ("dlsym [local] (foo): %s\n", dlerror ());
124 return 1;
127 local_getfoo = dlsym (local_handle, "getfoo");
128 if (local_getfoo == NULL)
130 printf ("dlsym [local] (getfoo): %s\n", dlerror ());
131 return 1;
134 local_setfoo = dlsym (local_handle, "setfoo");
135 if (local_setfoo == NULL)
137 printf ("dlsym [local] (setfoo): %s\n", dlerror ());
138 return 1;
141 /* Make sure we can get at the previously loaded module's symbols
142 via a handle that was obtained before the module was loaded too. */
143 initial_foop = dlsym (initial_handle, "foo");
144 if (initial_foop == NULL)
146 printf ("dlsym [initial] (foo): %s\n", dlerror ());
147 return 1;
150 initial_getfoo = dlsym (initial_handle, "getfoo");
151 if (initial_getfoo == NULL)
153 printf ("dlsym [initial] (getfoo): %s\n", dlerror ());
154 return 1;
157 initial_setfoo = dlsym (initial_handle, "setfoo");
158 if (initial_setfoo == NULL)
160 printf ("dlsym [initial] (setfoo): %s\n", dlerror ());
161 return 1;
164 /* Make sure the view of the initial state is consistent. */
165 foo = *initial_foop;
166 if (foo != MAGIC0)
168 printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC0);
169 return 1;
172 foo = *global_foop;
173 if (foo != MAGIC0)
175 printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC0);
176 return 1;
179 foo = *local_foop;
180 if (foo != MAGIC0)
182 printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC0);
183 return 1;
186 foo = initial_getfoo ();
187 if (foo != MAGIC0)
189 printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC0);
190 return 1;
193 foo = global_getfoo ();
194 if (foo != MAGIC0)
196 printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC0);
197 return 1;
200 foo = local_getfoo ();
201 if (foo != MAGIC0)
203 printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC0);
204 return 1;
207 /* Likewise with a change to its state made through the first handle. */
208 initial_setfoo (MAGIC1);
210 foo = *initial_foop;
211 if (foo != MAGIC1)
213 printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC1);
214 return 1;
217 foo = *global_foop;
218 if (foo != MAGIC1)
220 printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC1);
221 return 1;
224 foo = *local_foop;
225 if (foo != MAGIC1)
227 printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC1);
228 return 1;
231 foo = initial_getfoo ();
232 if (foo != MAGIC1)
234 printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC1);
235 return 1;
238 foo = global_getfoo ();
239 if (foo != MAGIC1)
241 printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC1);
242 return 1;
245 foo = local_getfoo ();
246 if (foo != MAGIC1)
248 printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC1);
249 return 1;
252 /* Likewise with a change to its state made through the second handle. */
253 global_setfoo (MAGIC2);
255 foo = *initial_foop;
256 if (foo != MAGIC2)
258 printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC2);
259 return 1;
262 foo = *global_foop;
263 if (foo != MAGIC2)
265 printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC2);
266 return 1;
269 foo = *local_foop;
270 if (foo != MAGIC2)
272 printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC2);
273 return 1;
276 foo = initial_getfoo ();
277 if (foo != MAGIC2)
279 printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC2);
280 return 1;
283 foo = global_getfoo ();
284 if (foo != MAGIC2)
286 printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC2);
287 return 1;
290 foo = local_getfoo ();
291 if (foo != MAGIC2)
293 printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC2);
294 return 1;
297 /* Likewise with a change to its state made through the third handle. */
298 local_setfoo (MAGIC3);
300 foo = *initial_foop;
301 if (foo != MAGIC3)
303 printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC3);
304 return 1;
307 foo = *global_foop;
308 if (foo != MAGIC3)
310 printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC3);
311 return 1;
314 foo = *local_foop;
315 if (foo != MAGIC3)
317 printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC3);
318 return 1;
321 foo = initial_getfoo ();
322 if (foo != MAGIC3)
324 printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC3);
325 return 1;
328 foo = global_getfoo ();
329 if (foo != MAGIC3)
331 printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC3);
332 return 1;
335 foo = local_getfoo ();
336 if (foo != MAGIC3)
338 printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC3);
339 return 1;
342 /* All done, clean up. */
343 initial_getfoo = NULL;
344 initial_setfoo = NULL;
345 initial_foop = NULL;
347 local_getfoo = NULL;
348 local_setfoo = NULL;
349 local_foop = NULL;
350 dlclose (local_handle);
352 global_getfoo = NULL;
353 global_setfoo = NULL;
354 global_foop = NULL;
355 dlclose (global_handle);
357 dlclose (initial_handle);
359 return 0;
362 #define TEST_FUNCTION do_test ()
363 #include "../test-skeleton.c"