aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/sigaltstack.sh
blob: cd8f7004decb87914ecddc070572fe842a03bdb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh

# sigaltstack(2) regression test by Steven Hartland <killing@multiplay.co.uk>
# Wrong altsigstack clearing on exec
# https://github.com/golang/go/issues/15658#issuecomment-287276856

# Fixed by r315453

cd /tmp
cat > test-sigs.c <<EOF
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>

extern char **environ;

static void
die(const char *s)
{
       perror(s);
       exit(EXIT_FAILURE);
}

static void
setstack(void *arg __unused)
{
	stack_t ss;

	ss.ss_sp = malloc(SIGSTKSZ);
	if (ss.ss_sp == NULL)
		die("malloc");

	ss.ss_size = SIGSTKSZ;
	ss.ss_flags = 0;
	if (sigaltstack(&ss, NULL) < 0)
		die("sigaltstack set");
}

static void *
thread_exec(void *arg)
{
	struct timespec ts = {0, 1000};
	char *argv[] = {"./test-sigs", "no-more", 0};

	setstack(arg);
	nanosleep(&ts, NULL);

	execve(argv[0], &argv[0], environ);
	die("exec failed");

	return NULL;
}

static void *
thread_sleep(void *arg __unused)
{
	sleep(10);

	return NULL;
}

int
main(int argc, char** argv __unused)
{
	int j;
	pthread_t tid1, tid2;

	if (argc != 1) {
		stack_t ss;

		if (sigaltstack(NULL, &ss) < 0)
			die("sigaltstack get");

		if (ss.ss_sp != NULL || ss.ss_flags != SS_DISABLE ||
		    ss.ss_size != 0) {
			fprintf(stderr, "invalid signal stack after execve: "
			    "ss_sp=%p ss_size=%lu ss_flags=0x%x\n", ss.ss_sp,
			    (unsigned long)ss.ss_size,
			    (unsigned int)ss.ss_flags);
			return 1;
		}

		printf("valid signal stack is valid after execve\n");

		return 0;
	}

	// We have to use two threads to ensure that can detect the
	// issue when new threads are added to the head (pre 269095)
	// and the tail of the process thread list.
	j = pthread_create(&tid1, NULL, thread_exec, NULL);
	if (j != 0) {
	       errno = j;
	       die("pthread_create");
	}

	j = pthread_create(&tid2, NULL, thread_sleep, NULL);
	if (j != 0) {
	       errno = j;
	       die("pthread_create");
	}

	j = pthread_join(tid1, NULL);
	if (j != 0) {
		errno = j;
		die("pthread_join");
	}

	j = pthread_join(tid2, NULL);
	if (j != 0) {
		errno = j;
	}

	return 0;
}
EOF

cc -o test-sigs -Wall -Wextra -O2 -g test-sigs.c -lpthread || exit 1
./test-sigs
s=$?

rm -f test-sigs test-sigs.c
exit $s