1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S E H - I N I T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2005-2013, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
31
32 /* This unit contains support for SEH (Structured Exception Handling).
33 Right now the only implementation is for Win32. */
34
35 #ifdef IN_RTS
36 #include "tconfig.h"
37 #include "tsystem.h"
38
39 /* We don't have libiberty, so use malloc. */
40 #define xmalloc(S) malloc (S)
41
42 #else
43 #include "config.h"
44 #include "system.h"
45 #endif
46
47 #include "raise.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /* Addresses of exception data blocks for predefined exceptions. */
54 extern struct Exception_Data constraint_error;
55 extern struct Exception_Data numeric_error;
56 extern struct Exception_Data program_error;
57 extern struct Exception_Data storage_error;
58 extern struct Exception_Data tasking_error;
59 extern struct Exception_Data _abort_signal;
60
61 #define Raise_From_Signal_Handler \
62 ada__exceptions__raise_from_signal_handler
63 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *)
64 ATTRIBUTE_NORETURN;
65
66
67 #if defined (_WIN32)
68
69 #include <windows.h>
70 #include <excpt.h>
71
72 /* Prototypes. */
73 extern void _global_unwind2 (void *);
74
75 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
76 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*) ATTRIBUTE_NORETURN;
77
78 struct Exception_Data *
79 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg);
80
81 /* Convert an SEH exception to an Ada one. Return the exception ID
82 and set MSG with the corresponding message. */
83
84 struct Exception_Data *
__gnat_map_SEH(EXCEPTION_RECORD * ExceptionRecord,const char ** msg)85 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg)
86 {
87 switch (ExceptionRecord->ExceptionCode)
88 {
89 case EXCEPTION_ACCESS_VIOLATION:
90 /* If the failing address isn't maximally-aligned or if the page
91 before the faulting page is not accessible, this is a program error.
92 */
93 if ((ExceptionRecord->ExceptionInformation[1] & 3) != 0
94 || IsBadCodePtr
95 ((FARPROC)(ExceptionRecord->ExceptionInformation[1] + 4096)))
96 {
97 *msg = "EXCEPTION_ACCESS_VIOLATION";
98 return &program_error;
99 }
100 else
101 {
102 /* otherwise it is a stack overflow */
103 *msg = "stack overflow or erroneous memory access";
104 return &storage_error;
105 }
106
107 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
108 *msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
109 return &constraint_error;
110
111 case EXCEPTION_DATATYPE_MISALIGNMENT:
112 *msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
113 return &constraint_error;
114
115 case EXCEPTION_FLT_DENORMAL_OPERAND:
116 *msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
117 return &constraint_error;
118
119 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
120 *msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
121 return &constraint_error;
122
123 case EXCEPTION_FLT_INVALID_OPERATION:
124 *msg = "EXCEPTION_FLT_INVALID_OPERATION";
125 return &constraint_error;
126
127 case EXCEPTION_FLT_OVERFLOW:
128 *msg = "EXCEPTION_FLT_OVERFLOW";
129 return &constraint_error;
130
131 case EXCEPTION_FLT_STACK_CHECK:
132 *msg = "EXCEPTION_FLT_STACK_CHECK";
133 return &program_error;
134
135 case EXCEPTION_FLT_UNDERFLOW:
136 *msg = "EXCEPTION_FLT_UNDERFLOW";
137 return &constraint_error;
138
139 case EXCEPTION_INT_DIVIDE_BY_ZERO:
140 *msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
141 return &constraint_error;
142
143 case EXCEPTION_INT_OVERFLOW:
144 *msg = "EXCEPTION_INT_OVERFLOW";
145 return &constraint_error;
146
147 case EXCEPTION_INVALID_DISPOSITION:
148 *msg = "EXCEPTION_INVALID_DISPOSITION";
149 return &program_error;
150
151 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
152 *msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
153 return &program_error;
154
155 case EXCEPTION_PRIV_INSTRUCTION:
156 *msg = "EXCEPTION_PRIV_INSTRUCTION";
157 return &program_error;
158
159 case EXCEPTION_SINGLE_STEP:
160 *msg = "EXCEPTION_SINGLE_STEP";
161 return &program_error;
162
163 case EXCEPTION_STACK_OVERFLOW:
164 *msg = "EXCEPTION_STACK_OVERFLOW";
165 return &storage_error;
166
167 default:
168 *msg = NULL;
169 return NULL;
170 }
171 }
172
173 #if !(defined (_WIN64) && defined (__SEH__))
174
175 EXCEPTION_DISPOSITION
__gnat_SEH_error_handler(struct _EXCEPTION_RECORD * ExceptionRecord,void * EstablisherFrame ATTRIBUTE_UNUSED,struct _CONTEXT * ContextRecord ATTRIBUTE_UNUSED,void * DispatcherContext ATTRIBUTE_UNUSED)176 __gnat_SEH_error_handler (struct _EXCEPTION_RECORD* ExceptionRecord,
177 void *EstablisherFrame ATTRIBUTE_UNUSED,
178 struct _CONTEXT* ContextRecord ATTRIBUTE_UNUSED,
179 void *DispatcherContext ATTRIBUTE_UNUSED)
180 {
181 struct Exception_Data *exception;
182 const char *msg;
183
184 exception = __gnat_map_SEH (ExceptionRecord, &msg);
185
186 if (exception == NULL)
187 {
188 exception = &program_error;
189 msg = "unhandled signal";
190 }
191
192 #if ! defined (_WIN64)
193 /* This call is important as it avoids locking the second time we catch a
194 signal. Note that this routine is documented as internal to Windows and
195 should not be used. */
196
197 _global_unwind2 (EstablisherFrame);
198 /* Call equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0); */
199 #endif
200
201 Raise_From_Signal_Handler (exception, msg);
202 }
203 #endif /* !(defined (_WIN64) && defined (__SEH__)) */
204
205 #if defined (_WIN64)
206 /* On x86_64 windows exception mechanism is no more based on a chained list
207 of handlers addresses on the stack. Instead unwinding information is used
208 to retrieve the exception handler (similar to ZCX GCC mechanism). So in
209 order to register an exception handler we need to put in the final
210 executable some unwinding information. This information might be present
211 statically in the image file inside the .pdata section or registered
212 through RtlAddFunctionTable API. Currently the GCC toolchain does not
213 generate the .pdata information for each function. As we don't need to
214 handle SEH exceptions except for signal handling we are registering a
215 "fake" unwinding data that associate a SEH exception handler to the
216 complete .text section. As we never return from the handler, the system
217 does not try to do the final unwinding using the pdata information. The
218 unwinding is handled by the runtime using either the GNAT SJLJ mechanism
219 or the ZCX GCC mechanism.
220
221 Solutions based on SetUnhandledExceptionFilter have been discarded as this
222 function is mostly disabled on last Windows versions.
223 Using AddVectoredExceptionHandler should also be discarded as it overrides
224 all SEH exception handlers that might be present in the program itself and
225 the loaded DLL (for example it results in unexpected behaviors in the
226 Win32 subsystem. */
227
228 #ifndef __SEH__
229 /* Don't use this trick when SEH are emitted by gcc, as it will conflict with
230 them. */
231 asm
232 (
233 " .section .rdata, \"dr\"\n"
234 " .align 4\n"
235 "unwind_info:\n"
236 " .byte 9\n" /* UNW_FLAG_EHANDLER | UNW_VERSION */
237 " .byte 0\n" /* Prologue size. */
238 " .byte 0\n" /* Count of unwind code. */
239 " .byte 0\n" /* Frame register and offset. */
240 " .rva __gnat_SEH_error_handler\n"
241 "\n"
242 " .section .pdata, \"dr\"\n"
243 " .align 4\n"
244 " .long 0\n" /* ImageBase */
245 " .rva etext\n"
246 " .rva unwind_info\n"
247 "\n"
248 " .text\n"
249 );
250 #endif /* __SEH__ */
251
__gnat_install_SEH_handler(void * eh ATTRIBUTE_UNUSED)252 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
253 {
254 /* Nothing to do, the handler is statically installed by the asm statement
255 just above. */
256 }
257
258 #else /* defined (_WIN64) */
259 /* Install the Win32 SEH exception handler. Note that the caller must have
260 allocated 8 bytes on the stack and pass the pointer to this stack
261 space. This is needed as the SEH exception handler must be on the stack of
262 the thread.
263
264 int buf[2];
265
266 __gnat_install_SEH_handler ((void*)buf);
267
268 main();
269
270 This call must be done before calling the main procedure or the thread
271 entry. The stack space must exists during all the main run. */
272
273 void
__gnat_install_SEH_handler(void * ER)274 __gnat_install_SEH_handler (void *ER)
275 {
276 int *ptr;
277
278 /* put current handler in ptr */
279
280 asm ("mov %%fs:(0),%0" : "=r" (ptr));
281
282 ((int *)ER)[0] = (int)ptr; /* previous handler */
283 ((int *)ER)[1] = (int)__gnat_SEH_error_handler; /* new handler */
284
285 /* ER is the new handler, set fs:(0) with this value */
286
287 asm volatile ("mov %0,%%fs:(0)": : "r" (ER));
288 }
289 #endif
290
291 #else /* defined (_WIN32) */
292 /* For all non Windows targets we provide a dummy SEH install handler. */
__gnat_install_SEH_handler(void * eh ATTRIBUTE_UNUSED)293 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
294 {
295 }
296 #endif
297
298 #ifdef __cplusplus
299 }
300 #endif
301