1 /* Test program for making nonexecutable stacks executable
2 on load of a DSO that requires executable stacks. */
10 #include <stackinfo.h>
17 asprintf (&cmd, "cat /proc/%d/maps", getpid ());
23 static void deeper (void (*f) (void));
29 tryme_thread (void *f)
31 (*((void (*) (void)) f)) ();
36 static pthread_barrier_t startup_barrier, go_barrier;
38 waiter_thread (void *arg)
41 pthread_barrier_wait (&startup_barrier);
42 pthread_barrier_wait (&go_barrier);
44 (*((void (*) (void)) *f)) ();
50 static bool allow_execstack = true;
56 /* Check whether SELinux is enabled and disallows executable stacks. */
57 FILE *fp = fopen ("/selinux/enforce", "r");
64 ssize_t n = getline (&line, &linelen, fp);
65 if (n > 0 && line[0] != '0')
72 fp = fopen ("/selinux/booleans/allow_execstack", "r");
75 n = getline (&line, &linelen, fp);
76 if (n > 0 && line[0] == '0')
77 allow_execstack = false;
84 printf ("executable stacks %sallowed\n", allow_execstack ? "" : "not ");
86 static void *f; /* Address of this is used in other threads. */
89 /* Create some threads while stacks are nonexecutable. */
93 pthread_barrier_init (&startup_barrier, NULL, N + 1);
94 pthread_barrier_init (&go_barrier, NULL, N + 1);
96 for (int i = 0; i < N; ++i)
98 int rc = pthread_create (&thr[i], NULL, &waiter_thread, &f);
100 error (1, rc, "pthread_create");
103 /* Make sure they are all there using their stacks. */
104 pthread_barrier_wait (&startup_barrier);
105 puts ("threads waiting");
111 void *old_stack_addr, *new_stack_addr;
113 pthread_t me = pthread_self ();
117 ret = pthread_getattr_np (me, &attr);
120 printf ("before execstack: pthread_getattr_np returned error: %s\n",
125 ret = pthread_attr_getstack (&attr, &old_stack_addr, &stack_size);
128 printf ("before execstack: pthread_attr_getstack returned error: %s\n",
132 # if _STACK_GROWS_DOWN
133 old_stack_addr += stack_size;
135 old_stack_addr -= stack_size;
139 /* Loading this module should force stacks to become executable. */
141 const char *soname = "tst-execstack-threads-mod.so";
143 const char *soname = "tst-execstack-mod.so";
145 void *h = dlopen (soname, RTLD_LAZY);
148 printf ("cannot load: %s\n", dlerror ());
149 return allow_execstack;
152 f = dlsym (h, "tryme");
155 printf ("symbol not found: %s\n", dlerror ());
159 /* Test if that really made our stack executable.
160 The `tryme' function should crash if not. */
162 (*((void (*) (void)) f)) ();
167 ret = pthread_getattr_np (me, &attr);
170 printf ("after execstack: pthread_getattr_np returned error: %s\n",
175 ret = pthread_attr_getstack (&attr, &new_stack_addr, &stack_size);
178 printf ("after execstack: pthread_attr_getstack returned error: %s\n",
183 # if _STACK_GROWS_DOWN
184 new_stack_addr += stack_size;
186 new_stack_addr -= stack_size;
189 /* It is possible that the dlopen'd module may have been mmapped just below
190 the stack. The stack size is taken as MIN(stack rlimit size, end of last
191 vma) in pthread_getattr_np. If rlimit is set high enough, it is possible
192 that the size may have changed. A subsequent call to
193 pthread_attr_getstack returns the size and (bottom - size) as the
194 stacksize and stackaddr respectively. If the size changes due to the
195 above, then both stacksize and stackaddr can change, but the stack bottom
196 should remain the same, which is computed as stackaddr + stacksize. */
197 if (old_stack_addr != new_stack_addr)
199 printf ("Stack end changed, old: %p, new: %p\n",
200 old_stack_addr, new_stack_addr);
203 printf ("Stack address remains the same: %p\n", old_stack_addr);
206 /* Test that growing the stack region gets new executable pages too. */
207 deeper ((void (*) (void)) f);
212 /* Test that a fresh thread now gets an executable stack. */
215 int rc = pthread_create (&th, NULL, &tryme_thread, f);
217 error (1, rc, "pthread_create");
221 /* The existing threads' stacks should have been changed.
222 Let them run to test it. */
223 pthread_barrier_wait (&go_barrier);
225 pthread_exit ((void *) (long int) (! allow_execstack));
228 return ! allow_execstack;
232 deeper (void (*f) (void))
234 char stack[1100 * 1024];
235 explicit_bzero (stack, sizeof stack);
237 memfrob (stack, sizeof stack);
241 #include <support/test-driver.c>