| 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-2025, 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 and Cygwin. */ |
| 34 |
|
| 35 |
#if defined (_WIN32) || (defined (__CYGWIN__) && defined (__SEH__)) |
| 36 |
/* Include system headers, before system.h poisons malloc. */ |
| 37 |
#define WIN32_LEAN_AND_MEAN |
| 38 |
#include <windows.h> |
| 39 |
#include <excpt.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef IN_RTS |
| 43 |
|
| 44 |
#include "runtime.h" |
| 45 |
|
| 46 |
/* We don't have libiberty, so use malloc. */ |
| 47 |
#define xmalloc(S) malloc (S) |
| 48 |
|
| 49 |
#else |
| 50 |
#include "config.h" |
| 51 |
#include "system.h" |
| 52 |
#endif |
| 53 |
|
| 54 |
#include "raise.h" |
| 55 |
|
| 56 |
#ifdef __cplusplus |
| 57 |
extern "C" { |
| 58 |
#endif |
| 59 |
|
| 60 |
/* Addresses of exception data blocks for predefined exceptions. */ |
| 61 |
extern struct Exception_Data constraint_error; |
| 62 |
extern struct Exception_Data numeric_error; |
| 63 |
extern struct Exception_Data program_error; |
| 64 |
extern struct Exception_Data storage_error; |
| 65 |
extern struct Exception_Data tasking_error; |
| 66 |
extern struct Exception_Data _abort_signal; |
| 67 |
|
| 68 |
#define Raise_From_Signal_Handler __gnat_raise_from_signal_handler |
| 69 |
extern void Raise_From_Signal_Handler (struct Exception_Data *, const void *) |
| 70 |
ATTRIBUTE_NORETURN; |
| 71 |
|
| 72 |
|
| 73 |
#if defined (_WIN32) || (defined (__CYGWIN__) && defined (__SEH__)) |
| 74 |
|
| 75 |
/* Prototypes. */ |
| 76 |
extern void _global_unwind2 (void *); |
| 77 |
|
| 78 |
EXCEPTION_DISPOSITION __gnat_SEH_error_handler |
| 79 |
(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*) ATTRIBUTE_NORETURN; |
| 80 |
|
| 81 |
struct Exception_Data * |
| 82 |
__gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg); |
| 83 |
|
| 84 |
/* Convert an SEH exception to an Ada one. Return the exception ID and set |
| 85 |
MSG to the corresponding message. */ |
| 86 |
|
| 87 |
struct Exception_Data * |
| 88 |
__gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg) |
| 89 |
{ |
| 90 |
switch (ExceptionRecord->ExceptionCode) |
| 91 |
{ |
| 92 |
case EXCEPTION_ACCESS_VIOLATION: |
| 93 |
/* If the failing address isn't maximally aligned or if the page before |
| 94 |
the faulting page is not accessible, this is a program error. */ |
| 95 |
if ((ExceptionRecord->ExceptionInformation[1] & 3) != 0 |
| 96 |
|| IsBadCodePtr |
| 97 |
((FARPROC)(ExceptionRecord->ExceptionInformation[1] + 4096))) |
| 98 |
{ |
| 99 |
*msg = "EXCEPTION_ACCESS_VIOLATION"; |
| 100 |
return &program_error; |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
/* Otherwise this is a stack overflow. */ |
| 105 |
*msg = "stack overflow or erroneous memory access"; |
| 106 |
return &storage_error; |
| 107 |
} |
| 108 |
|
| 109 |
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: |
| 110 |
*msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED"; |
| 111 |
return &constraint_error; |
| 112 |
|
| 113 |
case EXCEPTION_DATATYPE_MISALIGNMENT: |
| 114 |
*msg = "EXCEPTION_DATATYPE_MISALIGNMENT"; |
| 115 |
return &constraint_error; |
| 116 |
|
| 117 |
case EXCEPTION_FLT_DENORMAL_OPERAND: |
| 118 |
*msg = "EXCEPTION_FLT_DENORMAL_OPERAND"; |
| 119 |
return &constraint_error; |
| 120 |
|
| 121 |
case EXCEPTION_FLT_DIVIDE_BY_ZERO: |
| 122 |
*msg = "EXCEPTION_FLT_DENORMAL_OPERAND"; |
| 123 |
return &constraint_error; |
| 124 |
|
| 125 |
case EXCEPTION_FLT_INVALID_OPERATION: |
| 126 |
*msg = "EXCEPTION_FLT_INVALID_OPERATION"; |
| 127 |
return &constraint_error; |
| 128 |
|
| 129 |
case EXCEPTION_FLT_OVERFLOW: |
| 130 |
*msg = "EXCEPTION_FLT_OVERFLOW"; |
| 131 |
return &constraint_error; |
| 132 |
|
| 133 |
case EXCEPTION_FLT_STACK_CHECK: |
| 134 |
*msg = "EXCEPTION_FLT_STACK_CHECK"; |
| 135 |
return &program_error; |
| 136 |
|
| 137 |
case EXCEPTION_FLT_UNDERFLOW: |
| 138 |
*msg = "EXCEPTION_FLT_UNDERFLOW"; |
| 139 |
return &constraint_error; |
| 140 |
|
| 141 |
case EXCEPTION_INT_DIVIDE_BY_ZERO: |
| 142 |
*msg = "EXCEPTION_INT_DIVIDE_BY_ZERO"; |
| 143 |
return &constraint_error; |
| 144 |
|
| 145 |
case EXCEPTION_INT_OVERFLOW: |
| 146 |
*msg = "EXCEPTION_INT_OVERFLOW"; |
| 147 |
return &constraint_error; |
| 148 |
|
| 149 |
case EXCEPTION_INVALID_DISPOSITION: |
| 150 |
*msg = "EXCEPTION_INVALID_DISPOSITION"; |
| 151 |
return &program_error; |
| 152 |
|
| 153 |
case EXCEPTION_NONCONTINUABLE_EXCEPTION: |
| 154 |
*msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION"; |
| 155 |
return &program_error; |
| 156 |
|
| 157 |
case EXCEPTION_PRIV_INSTRUCTION: |
| 158 |
*msg = "EXCEPTION_PRIV_INSTRUCTION"; |
| 159 |
return &program_error; |
| 160 |
|
| 161 |
case EXCEPTION_SINGLE_STEP: |
| 162 |
*msg = "EXCEPTION_SINGLE_STEP"; |
| 163 |
return &program_error; |
| 164 |
|
| 165 |
case EXCEPTION_STACK_OVERFLOW: |
| 166 |
*msg = "EXCEPTION_STACK_OVERFLOW"; |
| 167 |
return &storage_error; |
| 168 |
|
| 169 |
default: |
| 170 |
*msg = NULL; |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#if !(defined (_WIN64) && defined (__SEH__)) |
| 176 |
|
| 177 |
/* The "fake" exception handler to be associated with the .text section. */ |
| 178 |
|
| 179 |
EXCEPTION_DISPOSITION |
| 180 |
__gnat_SEH_error_handler (struct _EXCEPTION_RECORD* ExceptionRecord, |
| 181 |
void *EstablisherFrame ATTRIBUTE_UNUSED, |
| 182 |
struct _CONTEXT* ContextRecord ATTRIBUTE_UNUSED, |
| 183 |
void *DispatcherContext ATTRIBUTE_UNUSED) |
| 184 |
{ |
| 185 |
struct Exception_Data *exception; |
| 186 |
const char *msg; |
| 187 |
|
| 188 |
exception = __gnat_map_SEH (ExceptionRecord, &msg); |
| 189 |
|
| 190 |
if (exception == NULL) |
| 191 |
{ |
| 192 |
exception = &program_error; |
| 193 |
msg = "unhandled signal"; |
| 194 |
} |
| 195 |
|
| 196 |
#if !defined (_WIN64) |
| 197 |
/* This call is important as it avoids locking the second time we catch a |
| 198 |
signal; it's equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0); |
| 199 |
Note that this routine is documented as internal to Windows and should |
| 200 |
not be used. */ |
| 201 |
_global_unwind2 (EstablisherFrame); |
| 202 |
#endif |
| 203 |
|
| 204 |
Raise_From_Signal_Handler (exception, msg); |
| 205 |
} |
| 206 |
|
| 207 |
#endif /* !(defined (_WIN64) && defined (__SEH__)) */ |
| 208 |
|
| 209 |
#if defined (_WIN64) |
| 210 |
|
| 211 |
/* On x86-64/Windows the EH mechanism is no more based on a chained list of |
| 212 |
handlers addresses on the stack. Instead unwinding information is used |
| 213 |
to retrieve the exception handler (similar to DWARF2 unwinding). So in |
| 214 |
order to register an exception handler, we need to put in the binary |
| 215 |
some unwinding information. This information can be present statically |
| 216 |
in the image file inside the .pdata section or registered through the |
| 217 |
RtlAddFunctionTable API. In the case where the GCC toolchain does not |
| 218 |
generate the .pdata information for each function, we don't really need |
| 219 |
to handle SEH exceptions except for signal handling, so we register a |
| 220 |
"fake" unwinding data that associates a SEH exception handler with the |
| 221 |
complete .text section. As we never return from the handler, the system |
| 222 |
does not try to do the final unwinding using the .pdata information and |
| 223 |
the unwinding is handled by the runtime using the GNAT or GCC mechanism. |
| 224 |
|
| 225 |
Solutions based on SetUnhandledExceptionFilter have been discarded as this |
| 226 |
function is mostly disabled on latest Windows versions. |
| 227 |
|
| 228 |
Using AddVectoredExceptionHandler should also be discarded as it overrides |
| 229 |
all SEH exception handlers that might be present in the program itself and |
| 230 |
the loaded DLL; for example it results in unexpected behavior in the Win32 |
| 231 |
subsystem. */ |
| 232 |
|
| 233 |
#ifndef __SEH__ |
| 234 |
|
| 235 |
/* Do not use this trick when GCC generates the .pdata information, since it |
| 236 |
is not necessary and will conflict with the per-function data. */ |
| 237 |
asm |
| 238 |
( |
| 239 |
" .section .rdata, \"dr\"\n" |
| 240 |
" .align 4\n" |
| 241 |
"unwind_info:\n" |
| 242 |
" .byte 9\n" /* UNW_FLAG_EHANDLER | UNW_VERSION */ |
| 243 |
" .byte 0\n" /* Prologue size. */ |
| 244 |
" .byte 0\n" /* Count of unwind code. */ |
| 245 |
" .byte 0\n" /* Frame register and offset. */ |
| 246 |
" .rva __gnat_SEH_error_handler\n" |
| 247 |
"\n" |
| 248 |
" .section .pdata, \"dr\"\n" |
| 249 |
" .align 4\n" |
| 250 |
" .long 0\n" /* ImageBase */ |
| 251 |
" .rva etext\n" |
| 252 |
" .rva unwind_info\n" |
| 253 |
"\n" |
| 254 |
" .text\n" |
| 255 |
); |
| 256 |
|
| 257 |
#endif /* __SEH__ */ |
| 258 |
|
| 259 |
/* Nothing to do, the handler is either not used or statically installed by |
| 260 |
the asm statement just above. */ |
| 261 |
void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED) |
| 262 |
{ |
| 263 |
} |
| 264 |
|
| 265 |
#else /* defined (_WIN64) */ |
| 266 |
|
| 267 |
/* Install the Win32 SEH exception handler. Note that the caller must have |
| 268 |
allocated 8 bytes on the stack and pass the pointer to this stack space. |
| 269 |
This is needed as the SEH exception handler must be on the stack of the |
| 270 |
thread. |
| 271 |
|
| 272 |
int buf[2]; |
| 273 |
|
| 274 |
__gnat_install_SEH_handler ((void*)buf); |
| 275 |
|
| 276 |
main(); |
| 277 |
|
| 278 |
This call must be done before calling the main procedure or the thread |
| 279 |
entry. The stack space must exist during the entire main run. */ |
| 280 |
|
| 281 |
void |
| 282 |
__gnat_install_SEH_handler (void *ER) |
| 283 |
{ |
| 284 |
int *ptr; |
| 285 |
|
| 286 |
/* Put current handler in PTR. */ |
| 287 |
asm ("mov %%fs:(0),%0" : "=r" (ptr)); |
| 288 |
|
| 289 |
((int *)ER)[0] = (int)ptr; /* previous handler */ |
| 290 |
((int *)ER)[1] = (int)__gnat_SEH_error_handler; /* new handler */ |
| 291 |
|
| 292 |
/* ER is the new handler, set fs:(0) to this value. */ |
| 293 |
asm volatile ("mov %0,%%fs:(0)": : "r" (ER)); |
| 294 |
} |
| 295 |
|
| 296 |
#endif |
| 297 |
|
| 298 |
#else /* defined (_WIN32) */ |
| 299 |
|
| 300 |
/* For all non-Windows targets we provide a dummy SEH install handler. */ |
| 301 |
void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED) |
| 302 |
{ |
| 303 |
} |
| 304 |
|
| 305 |
#endif |
| 306 |
|
| 307 |
#ifdef __cplusplus |
| 308 |
} |
| 309 |
#endif |