sigaltstack.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. //go:build !windows && !android
  5. // +build !windows,!android
  6. // Test that the Go runtime still works if C code changes the signal stack.
  7. package cgotest
  8. /*
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifdef _AIX
  14. // On AIX, SIGSTKSZ is too small to handle Go sighandler.
  15. #define CSIGSTKSZ 0x4000
  16. #else
  17. #define CSIGSTKSZ SIGSTKSZ
  18. #endif
  19. static stack_t oss;
  20. static char signalStack[CSIGSTKSZ];
  21. static void changeSignalStack(void) {
  22. stack_t ss;
  23. memset(&ss, 0, sizeof ss);
  24. ss.ss_sp = signalStack;
  25. ss.ss_flags = 0;
  26. ss.ss_size = CSIGSTKSZ;
  27. if (sigaltstack(&ss, &oss) < 0) {
  28. perror("sigaltstack");
  29. abort();
  30. }
  31. }
  32. static void restoreSignalStack(void) {
  33. #if (defined(__x86_64__) || defined(__i386__)) && defined(__APPLE__)
  34. // The Darwin C library enforces a minimum that the kernel does not.
  35. // This is OK since we allocated this much space in mpreinit,
  36. // it was just removed from the buffer by stackalloc.
  37. oss.ss_size = MINSIGSTKSZ;
  38. #endif
  39. if (sigaltstack(&oss, NULL) < 0) {
  40. perror("sigaltstack restore");
  41. abort();
  42. }
  43. }
  44. static int zero(void) {
  45. return 0;
  46. }
  47. */
  48. import "C"
  49. import (
  50. "runtime"
  51. "testing"
  52. )
  53. func testSigaltstack(t *testing.T) {
  54. switch {
  55. case runtime.GOOS == "solaris", runtime.GOOS == "illumos", runtime.GOOS == "ios" && runtime.GOARCH == "arm64":
  56. t.Skipf("switching signal stack not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
  57. }
  58. C.changeSignalStack()
  59. defer C.restoreSignalStack()
  60. defer func() {
  61. if recover() == nil {
  62. t.Error("did not see expected panic")
  63. }
  64. }()
  65. v := 1 / int(C.zero())
  66. t.Errorf("unexpected success of division by zero == %d", v)
  67. }