1 // SPDX-License-Identifier: GPL-2.0
3 * Test that signal delivery is able to expand the stack segment without
6 * Based on test code by Tom Lane.
13 #include <sys/types.h>
16 #include "../pmu/lib.h"
20 #define _MB (1024 * 1024)
22 static char *stack_base_ptr
;
23 static char *stack_top_ptr
;
25 static volatile sig_atomic_t sig_occurred
= 0;
27 static void sigusr1_handler(int signal_arg
)
32 static int consume_stack(unsigned int stack_size
, union pipe write_pipe
)
36 if ((stack_base_ptr
- &stack_cur
) < stack_size
)
37 return consume_stack(stack_size
, write_pipe
);
39 stack_top_ptr
= &stack_cur
;
41 FAIL_IF(notify_parent(write_pipe
));
50 static int child(unsigned int stack_size
, union pipe write_pipe
)
55 act
.sa_handler
= sigusr1_handler
;
56 sigemptyset(&act
.sa_mask
);
58 if (sigaction(SIGUSR1
, &act
, NULL
) < 0)
61 stack_base_ptr
= (char *) (((size_t) &stack_base
+ 65535) & ~65535UL);
63 FAIL_IF(consume_stack(stack_size
, write_pipe
));
65 printf("size 0x%06x: OK, stack base %p top %p (%zx used)\n",
66 stack_size
, stack_base_ptr
, stack_top_ptr
,
67 stack_base_ptr
- stack_top_ptr
);
72 static int test_one_size(unsigned int stack_size
)
74 union pipe read_pipe
, write_pipe
;
77 FAIL_IF(pipe(read_pipe
.fds
) == -1);
78 FAIL_IF(pipe(write_pipe
.fds
) == -1);
82 close(read_pipe
.read_fd
);
83 close(write_pipe
.write_fd
);
84 exit(child(stack_size
, read_pipe
));
87 close(read_pipe
.write_fd
);
88 close(write_pipe
.read_fd
);
89 FAIL_IF(sync_with_child(read_pipe
, write_pipe
));
93 FAIL_IF(wait_for_child(pid
));
95 close(read_pipe
.read_fd
);
96 close(write_pipe
.write_fd
);
103 unsigned int i
, size
;
105 // Test with used stack from 1MB - 64K to 1MB + 64K
106 // Increment by 64 to get more coverage of odd sizes
107 for (i
= 0; i
< (128 * _KB
); i
+= 64) {
108 size
= i
+ (1 * _MB
) - (64 * _KB
);
109 FAIL_IF(test_one_size(size
));
117 return test_harness(test
, "stack_expansion_signal");