0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <err.h>
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <signal.h>
0013 #include <sys/types.h>
0014 #include <unistd.h>
0015
0016 #include "../pmu/lib.h"
0017 #include "utils.h"
0018
0019 #define _KB (1024)
0020 #define _MB (1024 * 1024)
0021
0022 static char *stack_base_ptr;
0023 static char *stack_top_ptr;
0024
0025 static volatile sig_atomic_t sig_occurred = 0;
0026
0027 static void sigusr1_handler(int signal_arg)
0028 {
0029 sig_occurred = 1;
0030 }
0031
0032 static int consume_stack(unsigned int stack_size, union pipe write_pipe)
0033 {
0034 char stack_cur;
0035
0036 if ((stack_base_ptr - &stack_cur) < stack_size)
0037 return consume_stack(stack_size, write_pipe);
0038 else {
0039 stack_top_ptr = &stack_cur;
0040
0041 FAIL_IF(notify_parent(write_pipe));
0042
0043 while (!sig_occurred)
0044 barrier();
0045 }
0046
0047 return 0;
0048 }
0049
0050 static int child(unsigned int stack_size, union pipe write_pipe)
0051 {
0052 struct sigaction act;
0053 char stack_base;
0054
0055 act.sa_handler = sigusr1_handler;
0056 sigemptyset(&act.sa_mask);
0057 act.sa_flags = 0;
0058 if (sigaction(SIGUSR1, &act, NULL) < 0)
0059 err(1, "sigaction");
0060
0061 stack_base_ptr = (char *) (((size_t) &stack_base + 65535) & ~65535UL);
0062
0063 FAIL_IF(consume_stack(stack_size, write_pipe));
0064
0065 printf("size 0x%06x: OK, stack base %p top %p (%zx used)\n",
0066 stack_size, stack_base_ptr, stack_top_ptr,
0067 stack_base_ptr - stack_top_ptr);
0068
0069 return 0;
0070 }
0071
0072 static int test_one_size(unsigned int stack_size)
0073 {
0074 union pipe read_pipe, write_pipe;
0075 pid_t pid;
0076
0077 FAIL_IF(pipe(read_pipe.fds) == -1);
0078 FAIL_IF(pipe(write_pipe.fds) == -1);
0079
0080 pid = fork();
0081 if (pid == 0) {
0082 close(read_pipe.read_fd);
0083 close(write_pipe.write_fd);
0084 exit(child(stack_size, read_pipe));
0085 }
0086
0087 close(read_pipe.write_fd);
0088 close(write_pipe.read_fd);
0089 FAIL_IF(sync_with_child(read_pipe, write_pipe));
0090
0091 kill(pid, SIGUSR1);
0092
0093 FAIL_IF(wait_for_child(pid));
0094
0095 close(read_pipe.read_fd);
0096 close(write_pipe.write_fd);
0097
0098 return 0;
0099 }
0100
0101 int test(void)
0102 {
0103 unsigned int i, size;
0104
0105
0106
0107 for (i = 0; i < (128 * _KB); i += 64) {
0108 size = i + (1 * _MB) - (64 * _KB);
0109 FAIL_IF(test_one_size(size));
0110 }
0111
0112 return 0;
0113 }
0114
0115 int main(void)
0116 {
0117 return test_harness(test, "stack_expansion_signal");
0118 }