go-signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* go-signal.c -- signal handling for Go.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include <signal.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/time.h>
  9. #include "runtime.h"
  10. #include "go-assert.h"
  11. #include "go-panic.h"
  12. #include "signal_unix.h"
  13. #ifndef SA_RESTART
  14. #define SA_RESTART 0
  15. #endif
  16. #ifdef USING_SPLIT_STACK
  17. extern void __splitstack_getcontext(void *context[10]);
  18. extern void __splitstack_setcontext(void *context[10]);
  19. #endif
  20. #define N SigNotify
  21. #define K SigKill
  22. #define T SigThrow
  23. #define P SigPanic
  24. #define D SigDefault
  25. /* Signal actions. This collects the sigtab tables for several
  26. different targets from the master library. SIGKILL, SIGCONT, and
  27. SIGSTOP are not listed, as we don't want to set signal handlers for
  28. them. */
  29. SigTab runtime_sigtab[] = {
  30. #ifdef SIGHUP
  31. { SIGHUP, N + K },
  32. #endif
  33. #ifdef SIGINT
  34. { SIGINT, N + K },
  35. #endif
  36. #ifdef SIGQUIT
  37. { SIGQUIT, N + T },
  38. #endif
  39. #ifdef SIGILL
  40. { SIGILL, T },
  41. #endif
  42. #ifdef SIGTRAP
  43. { SIGTRAP, T },
  44. #endif
  45. #ifdef SIGABRT
  46. { SIGABRT, N + T },
  47. #endif
  48. #ifdef SIGBUS
  49. { SIGBUS, P },
  50. #endif
  51. #ifdef SIGFPE
  52. { SIGFPE, P },
  53. #endif
  54. #ifdef SIGUSR1
  55. { SIGUSR1, N },
  56. #endif
  57. #ifdef SIGSEGV
  58. { SIGSEGV, P },
  59. #endif
  60. #ifdef SIGUSR2
  61. { SIGUSR2, N },
  62. #endif
  63. #ifdef SIGPIPE
  64. { SIGPIPE, N },
  65. #endif
  66. #ifdef SIGALRM
  67. { SIGALRM, N },
  68. #endif
  69. #ifdef SIGTERM
  70. { SIGTERM, N + K },
  71. #endif
  72. #ifdef SIGSTKFLT
  73. { SIGSTKFLT, T },
  74. #endif
  75. #ifdef SIGCHLD
  76. { SIGCHLD, N },
  77. #endif
  78. #ifdef SIGTSTP
  79. { SIGTSTP, N + D },
  80. #endif
  81. #ifdef SIGTTIN
  82. { SIGTTIN, N + D },
  83. #endif
  84. #ifdef SIGTTOU
  85. { SIGTTOU, N + D },
  86. #endif
  87. #ifdef SIGURG
  88. { SIGURG, N },
  89. #endif
  90. #ifdef SIGXCPU
  91. { SIGXCPU, N },
  92. #endif
  93. #ifdef SIGXFSZ
  94. { SIGXFSZ, N },
  95. #endif
  96. #ifdef SIGVTALRM
  97. { SIGVTALRM, N },
  98. #endif
  99. #ifdef SIGPROF
  100. { SIGPROF, N },
  101. #endif
  102. #ifdef SIGWINCH
  103. { SIGWINCH, N },
  104. #endif
  105. #ifdef SIGIO
  106. { SIGIO, N },
  107. #endif
  108. #ifdef SIGPWR
  109. { SIGPWR, N },
  110. #endif
  111. #ifdef SIGSYS
  112. { SIGSYS, N },
  113. #endif
  114. #ifdef SIGEMT
  115. { SIGEMT, T },
  116. #endif
  117. #ifdef SIGINFO
  118. { SIGINFO, N },
  119. #endif
  120. #ifdef SIGTHR
  121. { SIGTHR, N },
  122. #endif
  123. { -1, 0 }
  124. };
  125. #undef N
  126. #undef K
  127. #undef T
  128. #undef P
  129. #undef D
  130. /* Handle a signal, for cases where we don't panic. We can split the
  131. stack here. */
  132. void
  133. runtime_sighandler (int sig, Siginfo *info,
  134. void *context __attribute__ ((unused)), G *gp)
  135. {
  136. M *m;
  137. int i;
  138. m = runtime_m ();
  139. #ifdef SIGPROF
  140. if (sig == SIGPROF)
  141. {
  142. if (m != NULL && gp != m->g0 && gp != m->gsignal)
  143. runtime_sigprof ();
  144. return;
  145. }
  146. #endif
  147. if (m == NULL)
  148. {
  149. runtime_badsignal (sig);
  150. return;
  151. }
  152. for (i = 0; runtime_sigtab[i].sig != -1; ++i)
  153. {
  154. SigTab *t;
  155. bool notify, crash;
  156. t = &runtime_sigtab[i];
  157. if (t->sig != sig)
  158. continue;
  159. notify = false;
  160. #ifdef SA_SIGINFO
  161. notify = info != NULL && info->si_code == SI_USER;
  162. #endif
  163. if (notify || (t->flags & SigNotify) != 0)
  164. {
  165. if (__go_sigsend (sig))
  166. return;
  167. }
  168. if ((t->flags & SigKill) != 0)
  169. runtime_exit (2);
  170. if ((t->flags & SigThrow) == 0)
  171. return;
  172. runtime_startpanic ();
  173. {
  174. const char *name = NULL;
  175. #ifdef HAVE_STRSIGNAL
  176. name = strsignal (sig);
  177. #endif
  178. if (name == NULL)
  179. runtime_printf ("Signal %d\n", sig);
  180. else
  181. runtime_printf ("%s\n", name);
  182. }
  183. if (m->lockedg != NULL && m->ncgo > 0 && gp == m->g0)
  184. {
  185. runtime_printf("signal arrived during cgo execution\n");
  186. gp = m->lockedg;
  187. }
  188. runtime_printf ("\n");
  189. if (runtime_gotraceback (&crash))
  190. {
  191. G *g;
  192. g = runtime_g ();
  193. runtime_traceback ();
  194. runtime_tracebackothers (g);
  195. /* The gc library calls runtime_dumpregs here, and provides
  196. a function that prints the registers saved in context in
  197. a readable form. */
  198. }
  199. if (crash)
  200. runtime_crash ();
  201. runtime_exit (2);
  202. }
  203. __builtin_unreachable ();
  204. }
  205. /* The start of handling a signal which panics. */
  206. static void
  207. sig_panic_leadin (G *gp)
  208. {
  209. int i;
  210. sigset_t clear;
  211. if (!runtime_canpanic (gp))
  212. runtime_throw ("unexpected signal during runtime execution");
  213. /* The signal handler blocked signals; unblock them. */
  214. i = sigfillset (&clear);
  215. __go_assert (i == 0);
  216. i = pthread_sigmask (SIG_UNBLOCK, &clear, NULL);
  217. __go_assert (i == 0);
  218. }
  219. #ifdef SA_SIGINFO
  220. /* Signal dispatch for signals which panic, on systems which support
  221. SA_SIGINFO. This is called on the thread stack, and as such it is
  222. permitted to split the stack. */
  223. static void
  224. sig_panic_info_handler (int sig, Siginfo *info, void *context)
  225. {
  226. G *g;
  227. g = runtime_g ();
  228. if (g == NULL || info->si_code == SI_USER)
  229. {
  230. runtime_sighandler (sig, info, context, g);
  231. return;
  232. }
  233. g->sig = sig;
  234. g->sigcode0 = info->si_code;
  235. g->sigcode1 = (uintptr_t) info->si_addr;
  236. /* It would be nice to set g->sigpc here as the gc library does, but
  237. I don't know how to get it portably. */
  238. sig_panic_leadin (g);
  239. switch (sig)
  240. {
  241. #ifdef SIGBUS
  242. case SIGBUS:
  243. if ((info->si_code == BUS_ADRERR && (uintptr_t) info->si_addr < 0x1000)
  244. || g->paniconfault)
  245. runtime_panicstring ("invalid memory address or "
  246. "nil pointer dereference");
  247. runtime_printf ("unexpected fault address %p\n", info->si_addr);
  248. runtime_throw ("fault");
  249. #endif
  250. #ifdef SIGSEGV
  251. case SIGSEGV:
  252. if (((info->si_code == 0
  253. || info->si_code == SEGV_MAPERR
  254. || info->si_code == SEGV_ACCERR)
  255. && (uintptr_t) info->si_addr < 0x1000)
  256. || g->paniconfault)
  257. runtime_panicstring ("invalid memory address or "
  258. "nil pointer dereference");
  259. runtime_printf ("unexpected fault address %p\n", info->si_addr);
  260. runtime_throw ("fault");
  261. #endif
  262. #ifdef SIGFPE
  263. case SIGFPE:
  264. switch (info->si_code)
  265. {
  266. case FPE_INTDIV:
  267. runtime_panicstring ("integer divide by zero");
  268. case FPE_INTOVF:
  269. runtime_panicstring ("integer overflow");
  270. }
  271. runtime_panicstring ("floating point error");
  272. #endif
  273. }
  274. /* All signals with SigPanic should be in cases above, and this
  275. handler should only be invoked for those signals. */
  276. __builtin_unreachable ();
  277. }
  278. #else /* !defined (SA_SIGINFO) */
  279. static void
  280. sig_panic_handler (int sig)
  281. {
  282. G *g;
  283. g = runtime_g ();
  284. if (g == NULL)
  285. {
  286. runtime_sighandler (sig, NULL, NULL, g);
  287. return;
  288. }
  289. g->sig = sig;
  290. g->sigcode0 = 0;
  291. g->sigcode1 = 0;
  292. sig_panic_leadin (g);
  293. switch (sig)
  294. {
  295. #ifdef SIGBUS
  296. case SIGBUS:
  297. runtime_panicstring ("invalid memory address or "
  298. "nil pointer dereference");
  299. #endif
  300. #ifdef SIGSEGV
  301. case SIGSEGV:
  302. runtime_panicstring ("invalid memory address or "
  303. "nil pointer dereference");
  304. #endif
  305. #ifdef SIGFPE
  306. case SIGFPE:
  307. runtime_panicstring ("integer divide by zero or floating point error");
  308. #endif
  309. }
  310. /* All signals with SigPanic should be in cases above, and this
  311. handler should only be invoked for those signals. */
  312. __builtin_unreachable ();
  313. }
  314. #endif /* !defined (SA_SIGINFO) */
  315. /* A signal handler used for signals which are not going to panic.
  316. This is called on the alternate signal stack so it may not split
  317. the stack. */
  318. static void
  319. sig_tramp_info (int, Siginfo *, void *) __attribute__ ((no_split_stack));
  320. static void
  321. sig_tramp_info (int sig, Siginfo *info, void *context)
  322. {
  323. G *gp;
  324. M *mp;
  325. #ifdef USING_SPLIT_STACK
  326. void *stack_context[10];
  327. #endif
  328. /* We are now running on the stack registered via sigaltstack.
  329. (Actually there is a small span of time between runtime_siginit
  330. and sigaltstack when the program starts.) */
  331. gp = runtime_g ();
  332. mp = runtime_m ();
  333. if (gp != NULL)
  334. {
  335. #ifdef USING_SPLIT_STACK
  336. __splitstack_getcontext (&stack_context[0]);
  337. #endif
  338. }
  339. if (gp != NULL && mp->gsignal != NULL)
  340. {
  341. /* We are running on the signal stack. Set the split stack
  342. context so that the stack guards are checked correctly. */
  343. #ifdef USING_SPLIT_STACK
  344. __splitstack_setcontext (&mp->gsignal->stack_context[0]);
  345. #endif
  346. }
  347. runtime_sighandler (sig, info, context, gp);
  348. /* We are going to return back to the signal trampoline and then to
  349. whatever we were doing before we got the signal. Restore the
  350. split stack context so that stack guards are checked
  351. correctly. */
  352. if (gp != NULL)
  353. {
  354. #ifdef USING_SPLIT_STACK
  355. __splitstack_setcontext (&stack_context[0]);
  356. #endif
  357. }
  358. }
  359. #ifndef SA_SIGINFO
  360. static void sig_tramp (int sig) __attribute__ ((no_split_stack));
  361. static void
  362. sig_tramp (int sig)
  363. {
  364. sig_tramp_info (sig, NULL, NULL);
  365. }
  366. #endif
  367. void
  368. runtime_setsig (int32 i, GoSighandler *fn, bool restart)
  369. {
  370. struct sigaction sa;
  371. int r;
  372. SigTab *t;
  373. memset (&sa, 0, sizeof sa);
  374. r = sigfillset (&sa.sa_mask);
  375. __go_assert (r == 0);
  376. t = &runtime_sigtab[i];
  377. if ((t->flags & SigPanic) == 0)
  378. {
  379. #ifdef SA_SIGINFO
  380. sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
  381. if (fn == runtime_sighandler)
  382. fn = (void *) sig_tramp_info;
  383. sa.sa_sigaction = (void *) fn;
  384. #else
  385. sa.sa_flags = SA_ONSTACK;
  386. if (fn == runtime_sighandler)
  387. fn = (void *) sig_tramp;
  388. sa.sa_handler = (void *) fn;
  389. #endif
  390. }
  391. else
  392. {
  393. #ifdef SA_SIGINFO
  394. sa.sa_flags = SA_SIGINFO;
  395. if (fn == runtime_sighandler)
  396. fn = (void *) sig_panic_info_handler;
  397. sa.sa_sigaction = (void *) fn;
  398. #else
  399. sa.sa_flags = 0;
  400. if (fn == runtime_sighandler)
  401. fn = (void *) sig_panic_handler;
  402. sa.sa_handler = (void *) fn;
  403. #endif
  404. }
  405. if (restart)
  406. sa.sa_flags |= SA_RESTART;
  407. if (sigaction (t->sig, &sa, NULL) != 0)
  408. __go_assert (0);
  409. }
  410. GoSighandler*
  411. runtime_getsig (int32 i)
  412. {
  413. struct sigaction sa;
  414. int r;
  415. SigTab *t;
  416. memset (&sa, 0, sizeof sa);
  417. r = sigemptyset (&sa.sa_mask);
  418. __go_assert (r == 0);
  419. t = &runtime_sigtab[i];
  420. if (sigaction (t->sig, NULL, &sa) != 0)
  421. runtime_throw ("sigaction read failure");
  422. if ((void *) sa.sa_handler == sig_tramp_info)
  423. return runtime_sighandler;
  424. #ifdef SA_SIGINFO
  425. if ((void *) sa.sa_handler == sig_panic_info_handler)
  426. return runtime_sighandler;
  427. #else
  428. if ((void *) sa.sa_handler == sig_tramp
  429. || (void *) sa.sa_handler == sig_panic_handler)
  430. return runtime_sighandler;
  431. #endif
  432. return (void *) sa.sa_handler;
  433. }
  434. /* Used by the os package to raise SIGPIPE. */
  435. void os_sigpipe (void) __asm__ (GOSYM_PREFIX "os.sigpipe");
  436. void
  437. os_sigpipe (void)
  438. {
  439. struct sigaction sa;
  440. int i;
  441. memset (&sa, 0, sizeof sa);
  442. sa.sa_handler = SIG_DFL;
  443. i = sigemptyset (&sa.sa_mask);
  444. __go_assert (i == 0);
  445. if (sigaction (SIGPIPE, &sa, NULL) != 0)
  446. abort ();
  447. raise (SIGPIPE);
  448. }
  449. void
  450. runtime_setprof(bool on)
  451. {
  452. USED(on);
  453. }