1 /* Test program for making nonexecutable stacks executable
2 on load of a DSO that requires executable stacks. */
15 asprintf (&cmd
, "cat /proc/%d/maps", getpid ());
21 static void deeper (void (*f
) (void));
27 tryme_thread (void *f
)
29 (*((void (*) (void)) f
)) ();
34 static pthread_barrier_t startup_barrier
, go_barrier
;
36 waiter_thread (void *arg
)
39 pthread_barrier_wait (&startup_barrier
);
40 pthread_barrier_wait (&go_barrier
);
42 (*((void (*) (void)) *f
)) ();
51 static void *f
; /* Address of this is used in other threads. */
54 /* Create some threads while stacks are nonexecutable. */
58 pthread_barrier_init (&startup_barrier
, NULL
, N
+ 1);
59 pthread_barrier_init (&go_barrier
, NULL
, N
+ 1);
61 for (int i
= 0; i
< N
; ++i
)
63 int rc
= pthread_create (&thr
[i
], NULL
, &waiter_thread
, &f
);
65 error (1, rc
, "pthread_create");
68 /* Make sure they are all there using their stacks. */
69 pthread_barrier_wait (&startup_barrier
);
70 puts ("threads waiting");
75 /* Loading this module should force stacks to become executable. */
76 void *h
= dlopen ("tst-execstack-mod.so", RTLD_LAZY
);
79 printf ("cannot load: %s\n", dlerror ());
83 f
= dlsym (h
, "tryme");
86 printf ("symbol not found: %s\n", dlerror ());
90 /* Test if that really made our stack executable.
91 The `tryme' function should crash if not. */
93 (*((void (*) (void)) f
)) ();
97 /* Test that growing the stack region gets new executable pages too. */
98 deeper ((void (*) (void)) f
);
103 /* Test that a fresh thread now gets an executable stack. */
106 int rc
= pthread_create (&th
, NULL
, &tryme_thread
, f
);
108 error (1, rc
, "pthread_create");
112 /* The existing threads' stacks should have been changed.
113 Let them run to test it. */
114 pthread_barrier_wait (&go_barrier
);
123 deeper (void (*f
) (void))
125 char stack
[1100 * 1024];
126 memfrob (stack
, sizeof stack
);
128 memfrob (stack
, sizeof stack
);
132 #define TEST_FUNCTION do_test ()
133 #include "../test-skeleton.c"