main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package main
  5. import "fmt"
  6. /*
  7. #cgo CFLAGS: -pthread
  8. #cgo LDFLAGS: -pthread
  9. #include <signal.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <pthread.h>
  14. int *p;
  15. static void sigsegv() {
  16. *p = 1;
  17. fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n");
  18. exit(2);
  19. }
  20. static void segvhandler(int signum) {
  21. if (signum == SIGSEGV) {
  22. fprintf(stdout, "ok\ttestsigfwd\n");
  23. exit(0); // success
  24. }
  25. }
  26. static volatile sig_atomic_t sigioSeen;
  27. // Use up some stack space.
  28. static void recur(int i, char *p) {
  29. char a[1024];
  30. *p = '\0';
  31. if (i > 0) {
  32. recur(i - 1, a);
  33. }
  34. }
  35. static void iohandler(int signum) {
  36. char a[1024];
  37. recur(4, a);
  38. sigioSeen = 1;
  39. }
  40. static void* sigioThread(void* arg __attribute__ ((unused))) {
  41. raise(SIGIO);
  42. return NULL;
  43. }
  44. static void sigioOnThread() {
  45. pthread_t tid;
  46. int i;
  47. pthread_create(&tid, NULL, sigioThread, NULL);
  48. pthread_join(tid, NULL);
  49. // Wait until the signal has been delivered.
  50. i = 0;
  51. while (!sigioSeen) {
  52. if (sched_yield() < 0) {
  53. perror("sched_yield");
  54. }
  55. i++;
  56. if (i > 10000) {
  57. fprintf(stderr, "looping too long waiting for signal\n");
  58. exit(EXIT_FAILURE);
  59. }
  60. }
  61. }
  62. static void __attribute__ ((constructor)) sigsetup(void) {
  63. struct sigaction act;
  64. memset(&act, 0, sizeof act);
  65. act.sa_handler = segvhandler;
  66. sigaction(SIGSEGV, &act, NULL);
  67. act.sa_handler = iohandler;
  68. sigaction(SIGIO, &act, NULL);
  69. }
  70. */
  71. import "C"
  72. var p *byte
  73. func f() (ret bool) {
  74. defer func() {
  75. if recover() == nil {
  76. fmt.Errorf("ERROR: couldn't raise SIGSEGV in Go.")
  77. C.exit(2)
  78. }
  79. ret = true
  80. }()
  81. *p = 1
  82. return false
  83. }
  84. func main() {
  85. // Test that the signal originating in Go is handled (and recovered) by Go.
  86. if !f() {
  87. fmt.Errorf("couldn't recover from SIGSEGV in Go.")
  88. C.exit(2)
  89. }
  90. // Test that the signal originating in C is handled by C.
  91. C.sigsegv()
  92. }