main4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.
  4. // Test that a signal handler that uses up stack space does not crash
  5. // if the signal is delivered to a thread running a goroutine.
  6. // This is a lot like misc/cgo/testcarchive/main2.c.
  7. #include <setjmp.h>
  8. #include <signal.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include <sched.h>
  16. #include <time.h>
  17. #include <dlfcn.h>
  18. static void die(const char* msg) {
  19. perror(msg);
  20. exit(EXIT_FAILURE);
  21. }
  22. static volatile sig_atomic_t sigioSeen;
  23. // Use up some stack space.
  24. static void recur(int i, char *p) {
  25. char a[1024];
  26. *p = '\0';
  27. if (i > 0) {
  28. recur(i - 1, a);
  29. }
  30. }
  31. // Signal handler that uses up more stack space than a goroutine will have.
  32. static void ioHandler(int signo, siginfo_t* info, void* ctxt) {
  33. char a[1024];
  34. recur(4, a);
  35. sigioSeen = 1;
  36. }
  37. static jmp_buf jmp;
  38. static char* nullPointer;
  39. // Signal handler for SIGSEGV on a C thread.
  40. static void segvHandler(int signo, siginfo_t* info, void* ctxt) {
  41. sigset_t mask;
  42. int i;
  43. if (sigemptyset(&mask) < 0) {
  44. die("sigemptyset");
  45. }
  46. if (sigaddset(&mask, SIGSEGV) < 0) {
  47. die("sigaddset");
  48. }
  49. i = sigprocmask(SIG_UNBLOCK, &mask, NULL);
  50. if (i != 0) {
  51. fprintf(stderr, "sigprocmask: %s\n", strerror(i));
  52. exit(EXIT_FAILURE);
  53. }
  54. // Don't try this at home.
  55. longjmp(jmp, signo);
  56. // We should never get here.
  57. abort();
  58. }
  59. int main(int argc, char** argv) {
  60. int verbose;
  61. struct sigaction sa;
  62. void* handle;
  63. void (*fn)(void);
  64. sigset_t mask;
  65. int i;
  66. struct timespec ts;
  67. verbose = argc > 2;
  68. setvbuf(stdout, NULL, _IONBF, 0);
  69. // Call setsid so that we can use kill(0, SIGIO) below.
  70. // Don't check the return value so that this works both from
  71. // a job control shell and from a shell script.
  72. setsid();
  73. if (verbose) {
  74. printf("calling sigaction\n");
  75. }
  76. memset(&sa, 0, sizeof sa);
  77. sa.sa_sigaction = ioHandler;
  78. if (sigemptyset(&sa.sa_mask) < 0) {
  79. die("sigemptyset");
  80. }
  81. sa.sa_flags = SA_SIGINFO;
  82. if (sigaction(SIGIO, &sa, NULL) < 0) {
  83. die("sigaction");
  84. }
  85. sa.sa_sigaction = segvHandler;
  86. if (sigaction(SIGSEGV, &sa, NULL) < 0 || sigaction(SIGBUS, &sa, NULL) < 0) {
  87. die("sigaction");
  88. }
  89. if (verbose) {
  90. printf("calling dlopen\n");
  91. }
  92. handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
  93. if (handle == NULL) {
  94. fprintf(stderr, "%s\n", dlerror());
  95. exit(EXIT_FAILURE);
  96. }
  97. if (verbose) {
  98. printf("calling dlsym\n");
  99. }
  100. // Start some goroutines.
  101. fn = (void(*)(void))dlsym(handle, "RunGoroutines");
  102. if (fn == NULL) {
  103. fprintf(stderr, "%s\n", dlerror());
  104. exit(EXIT_FAILURE);
  105. }
  106. if (verbose) {
  107. printf("calling RunGoroutines\n");
  108. }
  109. fn();
  110. // Block SIGIO in this thread to make it more likely that it
  111. // will be delivered to a goroutine.
  112. if (verbose) {
  113. printf("calling pthread_sigmask\n");
  114. }
  115. if (sigemptyset(&mask) < 0) {
  116. die("sigemptyset");
  117. }
  118. if (sigaddset(&mask, SIGIO) < 0) {
  119. die("sigaddset");
  120. }
  121. i = pthread_sigmask(SIG_BLOCK, &mask, NULL);
  122. if (i != 0) {
  123. fprintf(stderr, "pthread_sigmask: %s\n", strerror(i));
  124. exit(EXIT_FAILURE);
  125. }
  126. if (verbose) {
  127. printf("calling kill\n");
  128. }
  129. if (kill(0, SIGIO) < 0) {
  130. die("kill");
  131. }
  132. if (verbose) {
  133. printf("waiting for sigioSeen\n");
  134. }
  135. // Wait until the signal has been delivered.
  136. i = 0;
  137. while (!sigioSeen) {
  138. ts.tv_sec = 0;
  139. ts.tv_nsec = 1000000;
  140. nanosleep(&ts, NULL);
  141. i++;
  142. if (i > 5000) {
  143. fprintf(stderr, "looping too long waiting for signal\n");
  144. exit(EXIT_FAILURE);
  145. }
  146. }
  147. if (verbose) {
  148. printf("calling setjmp\n");
  149. }
  150. // Test that a SIGSEGV on this thread is delivered to us.
  151. if (setjmp(jmp) == 0) {
  152. if (verbose) {
  153. printf("triggering SIGSEGV\n");
  154. }
  155. *nullPointer = '\0';
  156. fprintf(stderr, "continued after address error\n");
  157. exit(EXIT_FAILURE);
  158. }
  159. if (verbose) {
  160. printf("calling dlsym\n");
  161. }
  162. // Make sure that a SIGSEGV in Go causes a run-time panic.
  163. fn = (void (*)(void))dlsym(handle, "TestSEGV");
  164. if (fn == NULL) {
  165. fprintf(stderr, "%s\n", dlerror());
  166. exit(EXIT_FAILURE);
  167. }
  168. if (verbose) {
  169. printf("calling TestSEGV\n");
  170. }
  171. fn();
  172. printf("PASS\n");
  173. return 0;
  174. }