1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Test that a signal handler that uses up stack space does not crash
6 // if the signal is delivered to a thread running a goroutine.
7 // This is a lot like misc/cgo/testcarchive/main2.c.
15 #include <sys/types.h>
21 static void die(const char* msg) {
26 static volatile sig_atomic_t sigioSeen;
28 // Use up some stack space.
29 static void recur(int i, char *p) {
38 // Signal handler that uses up more stack space than a goroutine will have.
39 static void ioHandler(int signo, siginfo_t* info, void* ctxt) {
47 static char* nullPointer;
49 // Signal handler for SIGSEGV on a C thread.
50 static void segvHandler(int signo, siginfo_t* info, void* ctxt) {
54 if (sigemptyset(&mask) < 0) {
57 if (sigaddset(&mask, SIGSEGV) < 0) {
60 i = sigprocmask(SIG_UNBLOCK, &mask, NULL);
62 fprintf(stderr, "sigprocmask: %s\n", strerror(i));
66 // Don't try this at home.
69 // We should never get here.
73 int main(int argc, char** argv) {
83 setvbuf(stdout, NULL, _IONBF, 0);
85 // Call setsid so that we can use kill(0, SIGIO) below.
86 // Don't check the return value so that this works both from
87 // a job control shell and from a shell script.
91 printf("calling sigaction\n");
94 memset(&sa, 0, sizeof sa);
95 sa.sa_sigaction = ioHandler;
96 if (sigemptyset(&sa.sa_mask) < 0) {
99 sa.sa_flags = SA_SIGINFO;
100 if (sigaction(SIGIO, &sa, NULL) < 0) {
104 sa.sa_sigaction = segvHandler;
105 if (sigaction(SIGSEGV, &sa, NULL) < 0 || sigaction(SIGBUS, &sa, NULL) < 0) {
110 printf("calling dlopen\n");
113 handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
114 if (handle == NULL) {
115 fprintf(stderr, "%s\n", dlerror());
120 printf("calling dlsym\n");
123 // Start some goroutines.
124 fn = (void(*)(void))dlsym(handle, "RunGoroutines");
126 fprintf(stderr, "%s\n", dlerror());
131 printf("calling RunGoroutines\n");
136 // Block SIGIO in this thread to make it more likely that it
137 // will be delivered to a goroutine.
140 printf("calling pthread_sigmask\n");
143 if (sigemptyset(&mask) < 0) {
146 if (sigaddset(&mask, SIGIO) < 0) {
149 i = pthread_sigmask(SIG_BLOCK, &mask, NULL);
151 fprintf(stderr, "pthread_sigmask: %s\n", strerror(i));
156 printf("calling kill\n");
159 if (kill(0, SIGIO) < 0) {
164 printf("waiting for sigioSeen\n");
167 // Wait until the signal has been delivered.
171 ts.tv_nsec = 1000000;
172 nanosleep(&ts, NULL);
175 fprintf(stderr, "looping too long waiting for signal\n");
181 printf("calling setjmp\n");
184 // Test that a SIGSEGV on this thread is delivered to us.
185 if (setjmp(jmp) == 0) {
187 printf("triggering SIGSEGV\n");
192 fprintf(stderr, "continued after address error\n");
197 printf("calling dlsym\n");
200 // Make sure that a SIGSEGV in Go causes a run-time panic.
201 fn = (void (*)(void))dlsym(handle, "TestSEGV");
203 fprintf(stderr, "%s\n", dlerror());
208 printf("calling TestSEGV\n");