mirror of git://sourceware.org/git/glibc.git
elf: Cleanup and improve tst-execstack
Reviewed-by: Florian Weimer <fweimer@redhat.com>
This commit is contained in:
parent
7962fa38e2
commit
ca96ea06b3
|
@ -1,24 +1,32 @@
|
||||||
/* Test program for making nonexecutable stacks executable
|
/* Test program for making nonexecutable stacks executable
|
||||||
on load of a DSO that requires executable stacks. */
|
on load of a DSO that requires executable stacks.
|
||||||
|
|
||||||
#include <dlfcn.h>
|
Copyright (C) 2003-2024 Free Software Foundation, Inc.
|
||||||
#include <stdbool.h>
|
This file is part of the GNU C Library.
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
The GNU C Library is free software; you can redistribute it and/or
|
||||||
#include <unistd.h>
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
The GNU C Library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with the GNU C Library; if not, see
|
||||||
|
<https://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
|
#include <array_length.h>
|
||||||
#include <error.h>
|
#include <error.h>
|
||||||
#include <stackinfo.h>
|
#include <stackinfo.h>
|
||||||
|
#include <stdbool.h>
|
||||||
static void
|
#include <string.h>
|
||||||
print_maps (void)
|
#include <support/xdlfcn.h>
|
||||||
{
|
#include <support/xthread.h>
|
||||||
#if 0
|
#include <support/check.h>
|
||||||
char *cmd = NULL;
|
#include <support/xstdio.h>
|
||||||
asprintf (&cmd, "cat /proc/%d/maps", getpid ());
|
|
||||||
system (cmd);
|
|
||||||
free (cmd);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void deeper (void (*f) (void));
|
static void deeper (void (*f) (void));
|
||||||
|
|
||||||
|
@ -38,8 +46,8 @@ static void *
|
||||||
waiter_thread (void *arg)
|
waiter_thread (void *arg)
|
||||||
{
|
{
|
||||||
void **f = arg;
|
void **f = arg;
|
||||||
pthread_barrier_wait (&startup_barrier);
|
xpthread_barrier_wait (&startup_barrier);
|
||||||
pthread_barrier_wait (&go_barrier);
|
xpthread_barrier_wait (&go_barrier);
|
||||||
|
|
||||||
(*((void (*) (void)) *f)) ();
|
(*((void (*) (void)) *f)) ();
|
||||||
|
|
||||||
|
@ -88,47 +96,26 @@ do_test (void)
|
||||||
#if USE_PTHREADS
|
#if USE_PTHREADS
|
||||||
/* Create some threads while stacks are nonexecutable. */
|
/* Create some threads while stacks are nonexecutable. */
|
||||||
#define N 5
|
#define N 5
|
||||||
pthread_t thr[N];
|
|
||||||
|
|
||||||
pthread_barrier_init (&startup_barrier, NULL, N + 1);
|
xpthread_barrier_init (&startup_barrier, NULL, N + 1);
|
||||||
pthread_barrier_init (&go_barrier, NULL, N + 1);
|
xpthread_barrier_init (&go_barrier, NULL, N + 1);
|
||||||
|
|
||||||
for (int i = 0; i < N; ++i)
|
for (int i = 0; i < N; ++i)
|
||||||
{
|
xpthread_create (NULL, &waiter_thread, &f);
|
||||||
int rc = pthread_create (&thr[i], NULL, &waiter_thread, &f);
|
|
||||||
if (rc)
|
|
||||||
error (1, rc, "pthread_create");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Make sure they are all there using their stacks. */
|
/* Make sure they are all there using their stacks. */
|
||||||
pthread_barrier_wait (&startup_barrier);
|
xpthread_barrier_wait (&startup_barrier);
|
||||||
puts ("threads waiting");
|
puts ("threads waiting");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
print_maps ();
|
|
||||||
|
|
||||||
#if USE_PTHREADS
|
#if USE_PTHREADS
|
||||||
void *old_stack_addr, *new_stack_addr;
|
void *old_stack_addr, *new_stack_addr;
|
||||||
size_t stack_size;
|
size_t stack_size;
|
||||||
pthread_t me = pthread_self ();
|
pthread_t me = pthread_self ();
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
int ret = 0;
|
TEST_VERIFY_EXIT (pthread_getattr_np (me, &attr) == 0);
|
||||||
|
TEST_VERIFY_EXIT (pthread_attr_getstack (&attr, &old_stack_addr,
|
||||||
ret = pthread_getattr_np (me, &attr);
|
&stack_size) == 0);
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
printf ("before execstack: pthread_getattr_np returned error: %s\n",
|
|
||||||
strerror (ret));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = pthread_attr_getstack (&attr, &old_stack_addr, &stack_size);
|
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
printf ("before execstack: pthread_attr_getstack returned error: %s\n",
|
|
||||||
strerror (ret));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
# if _STACK_GROWS_DOWN
|
# if _STACK_GROWS_DOWN
|
||||||
old_stack_addr += stack_size;
|
old_stack_addr += stack_size;
|
||||||
# else
|
# else
|
||||||
|
@ -149,36 +136,17 @@ do_test (void)
|
||||||
return allow_execstack;
|
return allow_execstack;
|
||||||
}
|
}
|
||||||
|
|
||||||
f = dlsym (h, "tryme");
|
f = xdlsym (h, "tryme");
|
||||||
if (f == NULL)
|
|
||||||
{
|
|
||||||
printf ("symbol not found: %s\n", dlerror ());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Test if that really made our stack executable.
|
/* Test if that really made our stack executable.
|
||||||
The `tryme' function should crash if not. */
|
The `tryme' function should crash if not. */
|
||||||
|
|
||||||
(*((void (*) (void)) f)) ();
|
(*((void (*) (void)) f)) ();
|
||||||
|
|
||||||
print_maps ();
|
|
||||||
|
|
||||||
#if USE_PTHREADS
|
#if USE_PTHREADS
|
||||||
ret = pthread_getattr_np (me, &attr);
|
TEST_VERIFY_EXIT (pthread_getattr_np (me, &attr) == 0);
|
||||||
if (ret)
|
TEST_VERIFY_EXIT (pthread_attr_getstack (&attr, &new_stack_addr,
|
||||||
{
|
&stack_size) == 0);
|
||||||
printf ("after execstack: pthread_getattr_np returned error: %s\n",
|
|
||||||
strerror (ret));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = pthread_attr_getstack (&attr, &new_stack_addr, &stack_size);
|
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
printf ("after execstack: pthread_attr_getstack returned error: %s\n",
|
|
||||||
strerror (ret));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
# if _STACK_GROWS_DOWN
|
# if _STACK_GROWS_DOWN
|
||||||
new_stack_addr += stack_size;
|
new_stack_addr += stack_size;
|
||||||
|
@ -194,33 +162,21 @@ do_test (void)
|
||||||
stacksize and stackaddr respectively. If the size changes due to the
|
stacksize and stackaddr respectively. If the size changes due to the
|
||||||
above, then both stacksize and stackaddr can change, but the stack bottom
|
above, then both stacksize and stackaddr can change, but the stack bottom
|
||||||
should remain the same, which is computed as stackaddr + stacksize. */
|
should remain the same, which is computed as stackaddr + stacksize. */
|
||||||
if (old_stack_addr != new_stack_addr)
|
TEST_VERIFY_EXIT (old_stack_addr == new_stack_addr);
|
||||||
{
|
|
||||||
printf ("Stack end changed, old: %p, new: %p\n",
|
|
||||||
old_stack_addr, new_stack_addr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printf ("Stack address remains the same: %p\n", old_stack_addr);
|
printf ("Stack address remains the same: %p\n", old_stack_addr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Test that growing the stack region gets new executable pages too. */
|
/* Test that growing the stack region gets new executable pages too. */
|
||||||
deeper ((void (*) (void)) f);
|
deeper ((void (*) (void)) f);
|
||||||
|
|
||||||
print_maps ();
|
|
||||||
|
|
||||||
#if USE_PTHREADS
|
#if USE_PTHREADS
|
||||||
/* Test that a fresh thread now gets an executable stack. */
|
/* Test that a fresh thread now gets an executable stack. */
|
||||||
{
|
xpthread_create (NULL, &tryme_thread, f);
|
||||||
pthread_t th;
|
|
||||||
int rc = pthread_create (&th, NULL, &tryme_thread, f);
|
|
||||||
if (rc)
|
|
||||||
error (1, rc, "pthread_create");
|
|
||||||
}
|
|
||||||
|
|
||||||
puts ("threads go");
|
puts ("threads go");
|
||||||
/* The existing threads' stacks should have been changed.
|
/* The existing threads' stacks should have been changed.
|
||||||
Let them run to test it. */
|
Let them run to test it. */
|
||||||
pthread_barrier_wait (&go_barrier);
|
xpthread_barrier_wait (&go_barrier);
|
||||||
|
|
||||||
pthread_exit ((void *) (long int) (! allow_execstack));
|
pthread_exit ((void *) (long int) (! allow_execstack));
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue