]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/tst-execstack.c
* sysdeps/i386/dl-machine.h (RTLD_START): Set __libc_stack_end before
[thirdparty/glibc.git] / elf / tst-execstack.c
CommitLineData
2abf9ff1
RM
1/* Test program for making nonexecutable stacks executable
2 on load of a DSO that requires executable stacks. */
3
4#include <dlfcn.h>
5#include <stdio.h>
6#include <string.h>
7#include <unistd.h>
8#include <error.h>
9
10static void
11print_maps (void)
12{
13#if 0
14 char *cmd = NULL;
15 asprintf (&cmd, "cat /proc/%d/maps", getpid ());
16 system (cmd);
17 free (cmd);
18#endif
19}
20
21static void deeper (void (*f) (void));
22
23#if USE_PTHREADS
24# include <pthread.h>
25
26static void *
27tryme_thread (void *f)
28{
29 (*((void (*) (void)) f)) ();
30
31 return 0;
32}
33
34static pthread_barrier_t startup_barrier, go_barrier;
35static void *
36waiter_thread (void *arg)
37{
38 void **f = arg;
39 pthread_barrier_wait (&startup_barrier);
40 pthread_barrier_wait (&go_barrier);
41
42 (*((void (*) (void)) *f)) ();
43
44 return 0;
45}
46#endif
47
48static int
49do_test (void)
50{
51 void *f;
52
53#if USE_PTHREADS
54 /* Create some threads while stacks are nonexecutable. */
55 #define N 5
56 pthread_t thr[N];
57
58 pthread_barrier_init (&startup_barrier, NULL, N + 1);
59 pthread_barrier_init (&go_barrier, NULL, N + 1);
60
61 for (int i = 0; i < N; ++i)
62 {
63 int rc = pthread_create (&thr[i], NULL, &waiter_thread, &f);
64 if (rc)
65 error (1, rc, "pthread_create");
66 }
67
68 /* Make sure they are all there using their stacks. */
69 pthread_barrier_wait (&startup_barrier);
70 puts ("threads waiting");
71#endif
72
73 print_maps ();
74
75 /* Loading this module should force stacks to become executable. */
76 void *h = dlopen ("tst-execstack-mod.so", RTLD_LAZY);
77 if (h == NULL)
78 {
79 printf ("cannot load: %s\n", dlerror ());
80 return 1;
81 }
82
83 f = dlsym (h, "tryme");
84 if (f == NULL)
85 {
86 printf ("symbol not found: %s\n", dlerror ());
87 return 1;
88 }
89
90 /* Test if that really made our stack executable.
91 The `tryme' function should crash if not. */
92
93 (*((void (*) (void)) f)) ();
94
95 print_maps ();
96
97 /* Test that growing the stack region gets new executable pages too. */
98 deeper ((void (*) (void)) f);
99
100 print_maps ();
101
102#if USE_PTHREADS
103 /* Test that a fresh thread now gets an executable stack. */
104 {
105 pthread_t th;
106 int rc = pthread_create (&th, NULL, &tryme_thread, f);
107 if (rc)
108 error (1, rc, "pthread_create");
109 }
110
111 puts ("threads go");
112 /* The existing threads' stacks should have been changed.
113 Let them run to test it. */
114 pthread_barrier_wait (&go_barrier);
115
116 pthread_exit (0);
117#endif
118
119 return 0;
120}
121
122static void
123deeper (void (*f) (void))
124{
125 char stack[1100 * 1024];
126 memfrob (stack, sizeof stack);
127 (*f) ();
128 memfrob (stack, sizeof stack);
129}
130
131
132#define TEST_FUNCTION do_test ()
133#include "../test-skeleton.c"