Debian Codegraph @

m4/nocrash.m4

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