nocrash.m4 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # nocrash.m4 serial 3
  2. dnl Copyright (C) 2005, 2009-2011 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. dnl Based on libsigsegv, from Bruno Haible and Paolo Bonzini.
  7. AC_PREREQ([2.13])
  8. dnl Expands to some code for use in .c programs that will cause the configure
  9. dnl test to exit instead of crashing. This is useful to avoid triggering
  10. dnl action from a background debugger and to avoid core dumps.
  11. dnl Usage: ...
  12. dnl ]GL_NOCRASH[
  13. dnl ...
  14. dnl int main() { nocrash_init(); ... }
  15. AC_DEFUN([GL_NOCRASH],[[
  16. #include <stdlib.h>
  17. #if defined __MACH__ && defined __APPLE__
  18. /* Avoid a crash on MacOS X. */
  19. #include <mach/mach.h>
  20. #include <mach/mach_error.h>
  21. #include <mach/thread_status.h>
  22. #include <mach/exception.h>
  23. #include <mach/task.h>
  24. #include <pthread.h>
  25. /* The exception port on which our thread listens. */
  26. static mach_port_t our_exception_port;
  27. /* The main function of the thread listening for exceptions of type
  28. EXC_BAD_ACCESS. */
  29. static void *
  30. mach_exception_thread (void *arg)
  31. {
  32. /* Buffer for a message to be received. */
  33. struct {
  34. mach_msg_header_t head;
  35. mach_msg_body_t msgh_body;
  36. char data[1024];
  37. } msg;
  38. mach_msg_return_t retval;
  39. /* Wait for a message on the exception port. */
  40. retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0, sizeof (msg),
  41. our_exception_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  42. if (retval != MACH_MSG_SUCCESS)
  43. abort ();
  44. exit (1);
  45. }
  46. static void
  47. nocrash_init (void)
  48. {
  49. mach_port_t self = mach_task_self ();
  50. /* Allocate a port on which the thread shall listen for exceptions. */
  51. if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
  52. == KERN_SUCCESS) {
  53. /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */
  54. if (mach_port_insert_right (self, our_exception_port, our_exception_port,
  55. MACH_MSG_TYPE_MAKE_SEND)
  56. == KERN_SUCCESS) {
  57. /* The exceptions we want to catch. Only EXC_BAD_ACCESS is interesting
  58. for us. */
  59. exception_mask_t mask = EXC_MASK_BAD_ACCESS;
  60. /* Create the thread listening on the exception port. */
  61. pthread_attr_t attr;
  62. pthread_t thread;
  63. if (pthread_attr_init (&attr) == 0
  64. && pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) == 0
  65. && pthread_create (&thread, &attr, mach_exception_thread, NULL) == 0) {
  66. pthread_attr_destroy (&attr);
  67. /* Replace the exception port info for these exceptions with our own.
  68. Note that we replace the exception port for the entire task, not only
  69. for a particular thread. This has the effect that when our exception
  70. port gets the message, the thread specific exception port has already
  71. been asked, and we don't need to bother about it.
  72. See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html. */
  73. task_set_exception_ports (self, mask, our_exception_port,
  74. EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
  75. }
  76. }
  77. }
  78. }
  79. #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  80. /* Avoid a crash on native Windows. */
  81. #define WIN32_LEAN_AND_MEAN
  82. #include <windows.h>
  83. #include <winerror.h>
  84. static LONG WINAPI
  85. exception_filter (EXCEPTION_POINTERS *ExceptionInfo)
  86. {
  87. switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
  88. {
  89. case EXCEPTION_ACCESS_VIOLATION:
  90. case EXCEPTION_IN_PAGE_ERROR:
  91. case EXCEPTION_STACK_OVERFLOW:
  92. case EXCEPTION_GUARD_PAGE:
  93. case EXCEPTION_PRIV_INSTRUCTION:
  94. case EXCEPTION_ILLEGAL_INSTRUCTION:
  95. case EXCEPTION_DATATYPE_MISALIGNMENT:
  96. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  97. case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  98. exit (1);
  99. }
  100. return EXCEPTION_CONTINUE_SEARCH;
  101. }
  102. static void
  103. nocrash_init (void)
  104. {
  105. SetUnhandledExceptionFilter ((LPTOP_LEVEL_EXCEPTION_FILTER) exception_filter);
  106. }
  107. #else
  108. /* Avoid a crash on POSIX systems. */
  109. #include <signal.h>
  110. /* A POSIX signal handler. */
  111. static void
  112. exception_handler (int sig)
  113. {
  114. exit (1);
  115. }
  116. static void
  117. nocrash_init (void)
  118. {
  119. #ifdef SIGSEGV
  120. signal (SIGSEGV, exception_handler);
  121. #endif
  122. #ifdef SIGBUS
  123. signal (SIGBUS, exception_handler);
  124. #endif
  125. }
  126. #endif
  127. ]])