| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . E X C E P T I O N S . E X C E P T I O N _ P R O P A G A T I O N -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 is the version using the GCC EH mechanism, which could rely on |
| 33 |
-- different underlying unwinding engines, for example DWARF or ARM unwind |
| 34 |
-- info based. Here is a sketch of the most prominent data structures |
| 35 |
-- involved: |
| 36 |
|
| 37 |
-- (s-excmac.ads) |
| 38 |
-- GNAT_GCC_Exception: |
| 39 |
-- *-----------------------------------* |
| 40 |
-- o-->| (s-excmac.ads) | |
| 41 |
-- | | Header : <gcc occurrence type> | |
| 42 |
-- | | - Class | |
| 43 |
-- | | ... | Constraint_Error: |
| 44 |
-- | |-----------------------------------* Program_Error: |
| 45 |
-- | | (a-except.ads) | Foreign_Exception: |
| 46 |
-- | | Occurrence : Exception_Occurrence | |
| 47 |
-- | | | (s-stalib. ads) |
| 48 |
-- | | - Id : Exception_Id --------------> Exception_Data |
| 49 |
-- o------ - Machine_Occurrence | *------------------------* |
| 50 |
-- | - Msg | | Not_Handled_By_Others | |
| 51 |
-- | - Traceback | | Lang | |
| 52 |
-- | ... | | Foreign_Data --o | |
| 53 |
-- *-----------------------------------* | Full_Name | | |
| 54 |
-- || | ... | | |
| 55 |
-- || foreign rtti blob *----------------|-------* |
| 56 |
-- || *---------------* | |
| 57 |
-- || | ... ... |<-------------------------o |
| 58 |
-- || *---------------* |
| 59 |
-- || |
| 60 |
-- Setup_Current_Excep() |
| 61 |
-- || |
| 62 |
-- || Latch into ATCB or |
| 63 |
-- || environment Current Exception Buffer: |
| 64 |
-- || |
| 65 |
-- vv |
| 66 |
-- <> : Exception_Occurrence |
| 67 |
-- *---------------------------* |
| 68 |
-- | ... ... ... ... ... ... * --- Get_Current_Excep() ----> |
| 69 |
-- *---------------------------* |
| 70 |
|
| 71 |
-- On "raise" events, the runtime allocates a new GNAT_GCC_Exception |
| 72 |
-- instance and eventually calls into libgcc's Unwind_RaiseException. |
| 73 |
-- This part handles the object through the header part only. |
| 74 |
|
| 75 |
-- During execution, Get_Current_Excep provides a pointer to the |
| 76 |
-- Exception_Occurrence being raised or last raised by the current task. |
| 77 |
|
| 78 |
-- This is actually the address of a statically allocated |
| 79 |
-- Exception_Occurrence attached to the current ATCB or to the environment |
| 80 |
-- thread into which an occurrence being raised is synchronized at critical |
| 81 |
-- points during the raise process, via Setup_Current_Excep. |
| 82 |
|
| 83 |
with Ada.Unchecked_Conversion; |
| 84 |
with Ada.Unchecked_Deallocation; |
| 85 |
|
| 86 |
with System.Storage_Elements; use System.Storage_Elements; |
| 87 |
with System.Exceptions.Machine; use System.Exceptions.Machine; |
| 88 |
|
| 89 |
separate (Ada.Exceptions) |
| 90 |
package body Exception_Propagation is |
| 91 |
|
| 92 |
use Exception_Traces; |
| 93 |
|
| 94 |
type bool is new Boolean; |
| 95 |
pragma Convention (C, bool); |
| 96 |
|
| 97 |
Foreign_Exception : aliased System.Standard_Library.Exception_Data; |
| 98 |
pragma Import (Ada, Foreign_Exception, |
| 99 |
"system__exceptions__foreign_exception"); |
| 100 |
-- Id for foreign exceptions |
| 101 |
|
| 102 |
-------------------------------------------------------------- |
| 103 |
-- GNAT Specific Entities To Deal With The GCC EH Circuitry -- |
| 104 |
-------------------------------------------------------------- |
| 105 |
|
| 106 |
-- Phase identifiers (Unwind Actions) |
| 107 |
|
| 108 |
type Unwind_Action is new Integer; |
| 109 |
pragma Convention (C, Unwind_Action); |
| 110 |
|
| 111 |
UA_SEARCH_PHASE : constant Unwind_Action := 1; |
| 112 |
UA_CLEANUP_PHASE : constant Unwind_Action := 2; |
| 113 |
UA_HANDLER_FRAME : constant Unwind_Action := 4; |
| 114 |
UA_FORCE_UNWIND : constant Unwind_Action := 8; |
| 115 |
UA_END_OF_STACK : constant Unwind_Action := 16; -- GCC extension |
| 116 |
|
| 117 |
pragma Unreferenced |
| 118 |
(UA_HANDLER_FRAME, |
| 119 |
UA_FORCE_UNWIND, |
| 120 |
UA_END_OF_STACK); |
| 121 |
|
| 122 |
procedure GNAT_GCC_Exception_Cleanup |
| 123 |
(Reason : Unwind_Reason_Code; |
| 124 |
Excep : not null GNAT_GCC_Exception_Access); |
| 125 |
pragma Convention (C, GNAT_GCC_Exception_Cleanup); |
| 126 |
-- Procedure called when a GNAT GCC exception is free. |
| 127 |
|
| 128 |
procedure Propagate_GCC_Exception |
| 129 |
(GCC_Exception : not null GCC_Exception_Access); |
| 130 |
pragma No_Return (Propagate_GCC_Exception); |
| 131 |
-- Propagate a GCC exception |
| 132 |
|
| 133 |
procedure Reraise_GCC_Exception |
| 134 |
(GCC_Exception : not null GCC_Exception_Access); |
| 135 |
pragma No_Return (Reraise_GCC_Exception); |
| 136 |
pragma Export (C, Reraise_GCC_Exception, "__gnat_reraise_zcx"); |
| 137 |
-- Called to implement raise without exception, ie reraise. Called |
| 138 |
-- directly from gigi. |
| 139 |
|
| 140 |
function Setup_Current_Excep |
| 141 |
(GCC_Exception : not null GCC_Exception_Access; |
| 142 |
Phase : Unwind_Action; |
| 143 |
Id : Exception_Id) return EOA; |
| 144 |
pragma Export (C, Setup_Current_Excep, "__gnat_setup_current_excep"); |
| 145 |
-- Acknowledge GCC_Exception as the current exception object being |
| 146 |
-- raised, which could be an Ada or a foreign exception object. Return |
| 147 |
-- a pointer to the embedded Ada occurrence for an Ada exception object, |
| 148 |
-- to the current exception buffer otherwise. |
| 149 |
-- |
| 150 |
-- Synchronize the current exception buffer as needed for possible |
| 151 |
-- accesses through Get_Current_Except.all afterwards, depending on the |
| 152 |
-- Phase bits, received either from the personality routine, from a |
| 153 |
-- forced_unwind cleanup handler, or just before the start of propagation |
| 154 |
-- for an Ada exception (Phase 0 in this case). |
| 155 |
|
| 156 |
procedure Unhandled_Except_Handler |
| 157 |
(GCC_Exception : not null GCC_Exception_Access); |
| 158 |
pragma No_Return (Unhandled_Except_Handler); |
| 159 |
pragma Export (C, Unhandled_Except_Handler, |
| 160 |
"__gnat_unhandled_except_handler"); |
| 161 |
-- Called for handle unhandled exceptions, ie the last chance handler |
| 162 |
-- on platforms (such as SEH) that never returns after throwing an |
| 163 |
-- exception. Called directly by gigi. |
| 164 |
|
| 165 |
function CleanupUnwind_Handler |
| 166 |
(UW_Version : Integer; |
| 167 |
UW_Phases : Unwind_Action; |
| 168 |
UW_Eclass : Exception_Class; |
| 169 |
UW_Exception : not null GCC_Exception_Access; |
| 170 |
UW_Context : System.Address; |
| 171 |
UW_Argument : System.Address) return Unwind_Reason_Code; |
| 172 |
pragma Import (C, CleanupUnwind_Handler, |
| 173 |
"__gnat_cleanupunwind_handler"); |
| 174 |
-- Hook called at each step of the forced unwinding we perform to trigger |
| 175 |
-- cleanups found during the propagation of an unhandled exception. |
| 176 |
|
| 177 |
-- GCC runtime functions used. These are C non-void functions, actually, |
| 178 |
-- but we ignore the return values. See raise.c as to why we are using |
| 179 |
-- __gnat stubs for these. |
| 180 |
|
| 181 |
procedure Unwind_RaiseException |
| 182 |
(UW_Exception : not null GCC_Exception_Access); |
| 183 |
pragma Import (C, Unwind_RaiseException, "__gnat_Unwind_RaiseException"); |
| 184 |
|
| 185 |
procedure Unwind_ForcedUnwind |
| 186 |
(UW_Exception : not null GCC_Exception_Access; |
| 187 |
UW_Handler : System.Address; |
| 188 |
UW_Argument : System.Address); |
| 189 |
pragma Import (C, Unwind_ForcedUnwind, "__gnat_Unwind_ForcedUnwind"); |
| 190 |
|
| 191 |
procedure Set_Exception_Parameter |
| 192 |
(Excep : EOA; |
| 193 |
GCC_Exception : not null GCC_Exception_Access); |
| 194 |
pragma Export |
| 195 |
(C, Set_Exception_Parameter, "__gnat_set_exception_parameter"); |
| 196 |
-- Called inserted by gigi to set the exception choice parameter from the |
| 197 |
-- gcc occurrence. |
| 198 |
|
| 199 |
procedure Set_Foreign_Occurrence |
| 200 |
(Excep : EOA; |
| 201 |
Mo : System.Address; |
| 202 |
Id : Exception_Id := Foreign_Exception'Access); |
| 203 |
-- Utility routine to initialize occurrence Excep from a foreign exception |
| 204 |
-- whose machine occurrence is Mo. The message is empty, the backtrace |
| 205 |
-- is empty too and the exception identity is Id. |
| 206 |
|
| 207 |
-- Hooks called when entering/leaving an exception handler for a |
| 208 |
-- given occurrence. The calls are generated by gigi in |
| 209 |
-- Exception_Handler_to_gnu_gcc. |
| 210 |
|
| 211 |
-- Begin_Handler_v1, called when entering an exception handler, |
| 212 |
-- claims responsibility for the handler to release the |
| 213 |
-- GCC_Exception occurrence. End_Handler_v1, called when |
| 214 |
-- leaving the handler, releases the occurrence, unless the |
| 215 |
-- occurrence is propagating further up, or the handler is |
| 216 |
-- dynamically nested in the context of another handler that |
| 217 |
-- claimed responsibility for releasing that occurrence. |
| 218 |
|
| 219 |
-- Responsibility is claimed by changing the Cleanup field to |
| 220 |
-- Claimed_Cleanup, which enables claimed exceptions to be |
| 221 |
-- recognized, and avoids accidental releases even by foreign |
| 222 |
-- handlers. |
| 223 |
|
| 224 |
function Begin_Handler_v1 |
| 225 |
(GCC_Exception : not null GCC_Exception_Access) |
| 226 |
return System.Address; |
| 227 |
pragma Export (C, Begin_Handler_v1, "__gnat_begin_handler_v1"); |
| 228 |
-- Called when entering an exception handler. Claim |
| 229 |
-- responsibility for releasing GCC_Exception, by setting the |
| 230 |
-- cleanup/release function to Claimed_Cleanup, and return the |
| 231 |
-- address of the previous cleanup/release function. |
| 232 |
|
| 233 |
procedure End_Handler_v1 |
| 234 |
(GCC_Exception : not null GCC_Exception_Access; |
| 235 |
Saved_Cleanup : System.Address; |
| 236 |
Propagating_Exception : GCC_Exception_Access); |
| 237 |
pragma Export (C, End_Handler_v1, "__gnat_end_handler_v1"); |
| 238 |
-- Called when leaving an exception handler. Restore the |
| 239 |
-- Saved_Cleanup in the GCC_Exception occurrence, and then release |
| 240 |
-- it, unless it remains claimed by an enclosing handler, or |
| 241 |
-- GCC_Exception and Propagating_Exception are the same |
| 242 |
-- occurrence. Propagating_Exception could be either an |
| 243 |
-- occurrence (re)raised within the handler of GCC_Exception, when |
| 244 |
-- we're executing as an exceptional cleanup, or null, if we're |
| 245 |
-- completing the handler of GCC_Exception normally. |
| 246 |
|
| 247 |
procedure Claimed_Cleanup |
| 248 |
(Reason : Unwind_Reason_Code; |
| 249 |
GCC_Exception : not null GCC_Exception_Access); |
| 250 |
pragma Export (C, Claimed_Cleanup, "__gnat_claimed_cleanup"); |
| 251 |
-- A do-nothing placeholder installed as GCC_Exception.Cleanup |
| 252 |
-- while handling GCC_Exception, to claim responsibility for |
| 253 |
-- releasing it, and to stop it from being accidentally released. |
| 254 |
|
| 255 |
-- The following are version 0 implementations of the version 1 |
| 256 |
-- hooks above. They remain in place for compatibility with the |
| 257 |
-- output of compilers that still use version 0, such as those |
| 258 |
-- used during bootstrap. They are interoperable with the v1 |
| 259 |
-- hooks, except that the older versions may malfunction when |
| 260 |
-- handling foreign exceptions passed to Reraise_Occurrence. |
| 261 |
|
| 262 |
procedure Begin_Handler (GCC_Exception : not null GCC_Exception_Access); |
| 263 |
pragma Export (C, Begin_Handler, "__gnat_begin_handler"); |
| 264 |
-- Called when entering an exception handler translated by an old |
| 265 |
-- compiler. It does nothing. |
| 266 |
|
| 267 |
procedure End_Handler (GCC_Exception : GCC_Exception_Access); |
| 268 |
pragma Export (C, End_Handler, "__gnat_end_handler"); |
| 269 |
-- Called when leaving an exception handler translated by an old |
| 270 |
-- compiler. It releases GCC_Exception, unless it is null. It is |
| 271 |
-- only ever null when the handler has a 'raise;' translated by a |
| 272 |
-- v0-using compiler. The artificial handler variable passed to |
| 273 |
-- End_Handler was set to null to tell End_Handler to refrain from |
| 274 |
-- releasing the reraised exception. In v1 safer ways are used to |
| 275 |
-- accomplish that. |
| 276 |
|
| 277 |
-------------------------------------------------------------------- |
| 278 |
-- Accessors to Basic Components of a GNAT Exception Data Pointer -- |
| 279 |
-------------------------------------------------------------------- |
| 280 |
|
| 281 |
-- As of today, these are only used by the C implementation of the GCC |
| 282 |
-- propagation personality routine to avoid having to rely on a C |
| 283 |
-- counterpart of the whole exception_data structure, which is both |
| 284 |
-- painful and error prone. These subprograms could be moved to a more |
| 285 |
-- widely visible location if need be. |
| 286 |
|
| 287 |
function Is_Handled_By_Others (E : Exception_Data_Ptr) return bool; |
| 288 |
pragma Export (C, Is_Handled_By_Others, "__gnat_is_handled_by_others"); |
| 289 |
|
| 290 |
function Language_For (E : Exception_Data_Ptr) return Character; |
| 291 |
pragma Export (C, Language_For, "__gnat_language_for"); |
| 292 |
|
| 293 |
function Foreign_Data_For (E : Exception_Data_Ptr) return Address; |
| 294 |
pragma Export (C, Foreign_Data_For, "__gnat_foreign_data_for"); |
| 295 |
|
| 296 |
function EID_For (GNAT_Exception : not null GNAT_GCC_Exception_Access) |
| 297 |
return Exception_Id; |
| 298 |
pragma Export (C, EID_For, "__gnat_eid_for"); |
| 299 |
|
| 300 |
--------------------------------------------------------------------------- |
| 301 |
-- Objects to materialize "others" and "all others" in the GCC EH tables -- |
| 302 |
--------------------------------------------------------------------------- |
| 303 |
|
| 304 |
-- Currently, these only have their address taken and compared so there is |
| 305 |
-- no real point having whole exception data blocks allocated. Note that |
| 306 |
-- there are corresponding declarations in gigi (trans.c) which must be |
| 307 |
-- kept properly synchronized. |
| 308 |
|
| 309 |
Others_Value : constant Character := 'O'; |
| 310 |
pragma Export (C, Others_Value, "__gnat_others_value"); |
| 311 |
|
| 312 |
All_Others_Value : constant Character := 'A'; |
| 313 |
pragma Export (C, All_Others_Value, "__gnat_all_others_value"); |
| 314 |
|
| 315 |
Unhandled_Others_Value : constant Character := 'U'; |
| 316 |
pragma Export (C, Unhandled_Others_Value, "__gnat_unhandled_others_value"); |
| 317 |
-- Special choice (emitted by gigi) to catch and notify unhandled |
| 318 |
-- exceptions on targets which always handle exceptions (such as SEH). |
| 319 |
-- The handler will simply call Unhandled_Except_Handler. |
| 320 |
|
| 321 |
------------------------- |
| 322 |
-- Allocate_Occurrence -- |
| 323 |
------------------------- |
| 324 |
|
| 325 |
function Allocate_Occurrence return EOA is |
| 326 |
Res : GNAT_GCC_Exception_Access; |
| 327 |
|
| 328 |
begin |
| 329 |
Res := New_Occurrence; |
| 330 |
Res.Header.Cleanup := GNAT_GCC_Exception_Cleanup'Address; |
| 331 |
Res.Occurrence.Machine_Occurrence := Res.all'Address; |
| 332 |
|
| 333 |
return Res.Occurrence'Access; |
| 334 |
end Allocate_Occurrence; |
| 335 |
|
| 336 |
-------------------------------- |
| 337 |
-- GNAT_GCC_Exception_Cleanup -- |
| 338 |
-------------------------------- |
| 339 |
|
| 340 |
procedure GNAT_GCC_Exception_Cleanup |
| 341 |
(Reason : Unwind_Reason_Code; |
| 342 |
Excep : not null GNAT_GCC_Exception_Access) |
| 343 |
is |
| 344 |
pragma Unreferenced (Reason); |
| 345 |
|
| 346 |
procedure Free is new Unchecked_Deallocation |
| 347 |
(GNAT_GCC_Exception, GNAT_GCC_Exception_Access); |
| 348 |
|
| 349 |
Copy : GNAT_GCC_Exception_Access := Excep; |
| 350 |
|
| 351 |
begin |
| 352 |
-- Simply free the memory |
| 353 |
|
| 354 |
Free (Copy); |
| 355 |
end GNAT_GCC_Exception_Cleanup; |
| 356 |
|
| 357 |
---------------------------- |
| 358 |
-- Set_Foreign_Occurrence -- |
| 359 |
---------------------------- |
| 360 |
|
| 361 |
procedure Set_Foreign_Occurrence |
| 362 |
(Excep : EOA; |
| 363 |
Mo : System.Address; |
| 364 |
Id : Exception_Id := Foreign_Exception'Access) is |
| 365 |
begin |
| 366 |
Excep.all := ( |
| 367 |
Id => Id, |
| 368 |
Machine_Occurrence => Mo, |
| 369 |
Msg => <>, |
| 370 |
Msg_Length => 0, |
| 371 |
Exception_Raised => True, |
| 372 |
Pid => Local_Partition_ID, |
| 373 |
Num_Tracebacks => 0, |
| 374 |
Tracebacks => <>); |
| 375 |
end Set_Foreign_Occurrence; |
| 376 |
|
| 377 |
------------------------- |
| 378 |
-- Setup_Current_Excep -- |
| 379 |
------------------------- |
| 380 |
|
| 381 |
function Setup_Current_Excep |
| 382 |
(GCC_Exception : not null GCC_Exception_Access; |
| 383 |
Phase : Unwind_Action; |
| 384 |
Id : Exception_Id) return EOA |
| 385 |
is |
| 386 |
Excep : constant EOA := Get_Current_Excep.all; |
| 387 |
|
| 388 |
begin |
| 389 |
|
| 390 |
if GCC_Exception.Class = GNAT_Exception_Class then |
| 391 |
|
| 392 |
-- Ada exception : latch the occurrence data in the Current |
| 393 |
-- Exception Buffer if needed and return a pointer to the original |
| 394 |
-- Ada exception object. This particular object was specifically |
| 395 |
-- allocated for this raise and is thus more precise than the fixed |
| 396 |
-- Current Exception Buffer address. |
| 397 |
|
| 398 |
declare |
| 399 |
GNAT_Occurrence : constant GNAT_GCC_Exception_Access := |
| 400 |
To_GNAT_GCC_Exception (GCC_Exception); |
| 401 |
begin |
| 402 |
|
| 403 |
-- When reaching here during SEARCH_PHASE, no need to |
| 404 |
-- replicate the copy performed at the propagation start. |
| 405 |
|
| 406 |
if Phase /= UA_SEARCH_PHASE then |
| 407 |
Excep.all := GNAT_Occurrence.Occurrence; |
| 408 |
end if; |
| 409 |
return GNAT_Occurrence.Occurrence'Access; |
| 410 |
end; |
| 411 |
|
| 412 |
else |
| 413 |
|
| 414 |
-- Foreign exception (caught by Ada handler, reaching here from |
| 415 |
-- personality routine) : The original exception object doesn't hold |
| 416 |
-- an Ada occurrence info. Set the foreign data pointer in the |
| 417 |
-- Current Exception Buffer and return the address of the latter. |
| 418 |
|
| 419 |
if Id = null then |
| 420 |
Set_Foreign_Occurrence (Excep, GCC_Exception.all'Address); |
| 421 |
else |
| 422 |
Set_Foreign_Occurrence (Excep, GCC_Exception.all'Address, Id); |
| 423 |
end if; |
| 424 |
|
| 425 |
return Excep; |
| 426 |
end if; |
| 427 |
end Setup_Current_Excep; |
| 428 |
|
| 429 |
---------------------- |
| 430 |
-- Begin_Handler_v1 -- |
| 431 |
---------------------- |
| 432 |
|
| 433 |
function Begin_Handler_v1 |
| 434 |
(GCC_Exception : not null GCC_Exception_Access) |
| 435 |
return System.Address is |
| 436 |
Saved_Cleanup : constant System.Address := GCC_Exception.Cleanup; |
| 437 |
begin |
| 438 |
-- Claim responsibility for releasing this exception, and stop |
| 439 |
-- others from releasing it. |
| 440 |
GCC_Exception.Cleanup := Claimed_Cleanup'Address; |
| 441 |
return Saved_Cleanup; |
| 442 |
end Begin_Handler_v1; |
| 443 |
|
| 444 |
-------------------- |
| 445 |
-- End_Handler_v1 -- |
| 446 |
-------------------- |
| 447 |
|
| 448 |
procedure End_Handler_v1 |
| 449 |
(GCC_Exception : not null GCC_Exception_Access; |
| 450 |
Saved_Cleanup : System.Address; |
| 451 |
Propagating_Exception : GCC_Exception_Access) is |
| 452 |
begin |
| 453 |
GCC_Exception.Cleanup := Saved_Cleanup; |
| 454 |
-- Restore the Saved_Cleanup, so that it is either used to |
| 455 |
-- release GCC_Exception below, or transferred to the next |
| 456 |
-- handler of the Propagating_Exception occurrence. The |
| 457 |
-- following test ensures that an occurrence is only released |
| 458 |
-- once, even after reraises. |
| 459 |
-- |
| 460 |
-- The idea is that the GCC_Exception is not to be released |
| 461 |
-- unless it had an unclaimed Cleanup when the handler started |
| 462 |
-- (see Begin_Handler_v1 above), but if we propagate across its |
| 463 |
-- handler a reraise of the same exception, we transfer to the |
| 464 |
-- Propagating_Exception the responsibility for running the |
| 465 |
-- Saved_Cleanup when its handler completes. |
| 466 |
-- |
| 467 |
-- This ownership transfer mechanism ensures safety, as in |
| 468 |
-- single release and no dangling pointers, because there is no |
| 469 |
-- way to hold on to the Machine_Occurrence of an |
| 470 |
-- Exception_Occurrence: the only situations in which another |
| 471 |
-- Exception_Occurrence gets the same Machine_Occurrence are |
| 472 |
-- through Reraise_Occurrence, and plain reraise, and so we |
| 473 |
-- have the following possibilities: |
| 474 |
-- |
| 475 |
-- - Reraise_Occurrence is handled within the running handler, |
| 476 |
-- and so when completing the dynamically nested handler, we |
| 477 |
-- must NOT release the exception. A Claimed_Cleanup upon |
| 478 |
-- entry of the nested handler, installed when entering the |
| 479 |
-- enclosing handler, ensures the exception will not be |
| 480 |
-- released by the nested handler, but rather by the enclosing |
| 481 |
-- handler. |
| 482 |
-- |
| 483 |
-- - Reraise_Occurrence/reraise escapes the running handler, |
| 484 |
-- and we run as an exceptional cleanup for GCC_Exception. The |
| 485 |
-- Saved_Cleanup was reinstalled, but since we're propagating |
| 486 |
-- the same machine occurrence, we do not release it. Instead, |
| 487 |
-- we transfer responsibility for releasing it to the eventual |
| 488 |
-- handler of the propagating exception. |
| 489 |
-- |
| 490 |
-- - An unrelated exception propagates through the running |
| 491 |
-- handler. We restored GCC_Exception.Saved_Cleanup above. |
| 492 |
-- Since we're propagating a different exception, we proceed to |
| 493 |
-- release GCC_Exception, unless Saved_Cleanup was |
| 494 |
-- Claimed_Cleanup, because then we know we're not in the |
| 495 |
-- outermost handler for GCC_Exception. |
| 496 |
-- |
| 497 |
-- - The handler completes normally, so it reinstalls the |
| 498 |
-- Saved_Cleanup and runs it, unless it was Claimed_Cleanup. |
| 499 |
-- If Saved_Cleanup is null, Unwind_DeleteException (currently) |
| 500 |
-- has no effect, so we could skip it, but if it is ever |
| 501 |
-- changed to do more in this case, we're ready for that, |
| 502 |
-- calling it exactly once. |
| 503 |
if Saved_Cleanup /= Claimed_Cleanup'Address |
| 504 |
and then |
| 505 |
Propagating_Exception /= GCC_Exception |
| 506 |
then |
| 507 |
declare |
| 508 |
Current : constant EOA := Get_Current_Excep.all; |
| 509 |
Cur_Occ : constant GCC_Exception_Access := |
| 510 |
To_GCC_Exception (Current.Machine_Occurrence); |
| 511 |
begin |
| 512 |
-- If we are releasing the Machine_Occurrence of the current |
| 513 |
-- exception, reset the access to it, so that it is no |
| 514 |
-- longer accessible. |
| 515 |
if Cur_Occ = GCC_Exception then |
| 516 |
Current.Machine_Occurrence := System.Null_Address; |
| 517 |
end if; |
| 518 |
end; |
| 519 |
Unwind_DeleteException (GCC_Exception); |
| 520 |
end if; |
| 521 |
end End_Handler_v1; |
| 522 |
|
| 523 |
--------------------- |
| 524 |
-- Claimed_Cleanup -- |
| 525 |
--------------------- |
| 526 |
|
| 527 |
procedure Claimed_Cleanup |
| 528 |
(Reason : Unwind_Reason_Code; |
| 529 |
GCC_Exception : not null GCC_Exception_Access) is |
| 530 |
pragma Unreferenced (Reason); |
| 531 |
pragma Unreferenced (GCC_Exception); |
| 532 |
begin |
| 533 |
-- This procedure should never run. If it does, it's either a |
| 534 |
-- version 0 handler or a foreign handler, attempting to |
| 535 |
-- release an exception while a version 1 handler that claimed |
| 536 |
-- responsibility for releasing the exception remains still |
| 537 |
-- active. This placeholder stops GCC_Exception from being |
| 538 |
-- released by them. |
| 539 |
|
| 540 |
-- We could get away with just Null_Address instead, with |
| 541 |
-- nearly the same effect, but with this placeholder we can |
| 542 |
-- detect and report unexpected releases, and we can tell apart |
| 543 |
-- a GCC_Exception without a Cleanup, from one with another |
| 544 |
-- active handler, so as to still call Unwind_DeleteException |
| 545 |
-- exactly once: currently, Unwind_DeleteException does nothing |
| 546 |
-- when the Cleanup is null, but should it ever be changed to |
| 547 |
-- do more, we'll still be safe. |
| 548 |
null; |
| 549 |
end Claimed_Cleanup; |
| 550 |
|
| 551 |
------------------- |
| 552 |
-- Begin_Handler -- |
| 553 |
------------------- |
| 554 |
|
| 555 |
procedure Begin_Handler (GCC_Exception : not null GCC_Exception_Access) is |
| 556 |
pragma Unreferenced (GCC_Exception); |
| 557 |
begin |
| 558 |
null; |
| 559 |
end Begin_Handler; |
| 560 |
|
| 561 |
----------------- |
| 562 |
-- End_Handler -- |
| 563 |
----------------- |
| 564 |
|
| 565 |
procedure End_Handler (GCC_Exception : GCC_Exception_Access) is |
| 566 |
begin |
| 567 |
if GCC_Exception /= null then |
| 568 |
|
| 569 |
-- The exception might have been reraised, in this case the cleanup |
| 570 |
-- mustn't be called. |
| 571 |
|
| 572 |
Unwind_DeleteException (GCC_Exception); |
| 573 |
end if; |
| 574 |
end End_Handler; |
| 575 |
|
| 576 |
----------------------------- |
| 577 |
-- Reraise_GCC_Exception -- |
| 578 |
----------------------------- |
| 579 |
|
| 580 |
procedure Reraise_GCC_Exception |
| 581 |
(GCC_Exception : not null GCC_Exception_Access) |
| 582 |
is |
| 583 |
begin |
| 584 |
-- Simply propagate it |
| 585 |
|
| 586 |
Propagate_GCC_Exception (GCC_Exception); |
| 587 |
end Reraise_GCC_Exception; |
| 588 |
|
| 589 |
----------------------------- |
| 590 |
-- Propagate_GCC_Exception -- |
| 591 |
----------------------------- |
| 592 |
|
| 593 |
-- Call Unwind_RaiseException to actually throw, taking care of handling |
| 594 |
-- the two phase scheme it implements. |
| 595 |
|
| 596 |
procedure Propagate_GCC_Exception |
| 597 |
(GCC_Exception : not null GCC_Exception_Access) |
| 598 |
is |
| 599 |
-- Acknowledge the current exception info now, before unwinding |
| 600 |
-- starts so it is available even from C++ handlers involved before |
| 601 |
-- our personality routine. |
| 602 |
|
| 603 |
Excep : constant EOA := |
| 604 |
Setup_Current_Excep (GCC_Exception, Phase => 0, Id => null); |
| 605 |
|
| 606 |
begin |
| 607 |
-- Perform a standard raise first. If a regular handler is found, it |
| 608 |
-- will be entered after all the intermediate cleanups have run. If |
| 609 |
-- there is no regular handler, it will return. |
| 610 |
|
| 611 |
Unwind_RaiseException (GCC_Exception); |
| 612 |
|
| 613 |
-- If we get here we know the exception is not handled, as otherwise |
| 614 |
-- Unwind_RaiseException arranges for the handler to be entered. Take |
| 615 |
-- the necessary steps to enable the debugger to gain control while the |
| 616 |
-- stack is still intact. |
| 617 |
|
| 618 |
Notify_Unhandled_Exception (Excep); |
| 619 |
|
| 620 |
-- Now, un a forced unwind to trigger cleanups. Control should not |
| 621 |
-- resume there, if there are cleanups and in any cases as the |
| 622 |
-- unwinding hook calls Unhandled_Exception_Terminate when end of |
| 623 |
-- stack is reached. |
| 624 |
|
| 625 |
Unwind_ForcedUnwind |
| 626 |
(GCC_Exception, |
| 627 |
CleanupUnwind_Handler'Address, |
| 628 |
System.Null_Address); |
| 629 |
|
| 630 |
-- We get here in case of error. The debugger has been notified before |
| 631 |
-- the second step above. |
| 632 |
|
| 633 |
Unhandled_Except_Handler (GCC_Exception); |
| 634 |
end Propagate_GCC_Exception; |
| 635 |
|
| 636 |
------------------------- |
| 637 |
-- Propagate_Exception -- |
| 638 |
------------------------- |
| 639 |
|
| 640 |
procedure Propagate_Exception (Excep : Exception_Occurrence) is |
| 641 |
begin |
| 642 |
Propagate_GCC_Exception (To_GCC_Exception (Excep.Machine_Occurrence)); |
| 643 |
end Propagate_Exception; |
| 644 |
|
| 645 |
----------------------------- |
| 646 |
-- Set_Exception_Parameter -- |
| 647 |
----------------------------- |
| 648 |
|
| 649 |
procedure Set_Exception_Parameter |
| 650 |
(Excep : EOA; |
| 651 |
GCC_Exception : not null GCC_Exception_Access) |
| 652 |
is |
| 653 |
begin |
| 654 |
-- Setup the exception occurrence |
| 655 |
|
| 656 |
if GCC_Exception.Class = GNAT_Exception_Class then |
| 657 |
|
| 658 |
-- From the GCC exception |
| 659 |
|
| 660 |
declare |
| 661 |
GNAT_Occurrence : constant GNAT_GCC_Exception_Access := |
| 662 |
To_GNAT_GCC_Exception (GCC_Exception); |
| 663 |
begin |
| 664 |
Save_Occurrence (Excep.all, GNAT_Occurrence.Occurrence); |
| 665 |
end; |
| 666 |
|
| 667 |
else |
| 668 |
-- A default one. Take the Id from the exception object |
| 669 |
-- created by Setup_Current_Excep. |
| 670 |
|
| 671 |
Set_Foreign_Occurrence (Excep, GCC_Exception.all'Address, |
| 672 |
Get_Current_Excep.all.Id); |
| 673 |
|
| 674 |
end if; |
| 675 |
end Set_Exception_Parameter; |
| 676 |
|
| 677 |
------------------------------ |
| 678 |
-- Unhandled_Except_Handler -- |
| 679 |
------------------------------ |
| 680 |
|
| 681 |
procedure Unhandled_Except_Handler |
| 682 |
(GCC_Exception : not null GCC_Exception_Access) |
| 683 |
is |
| 684 |
Excep : EOA; |
| 685 |
begin |
| 686 |
Excep := Setup_Current_Excep (GCC_Exception, Phase => UA_CLEANUP_PHASE, |
| 687 |
Id => null); |
| 688 |
Unhandled_Exception_Terminate (Excep); |
| 689 |
end Unhandled_Except_Handler; |
| 690 |
|
| 691 |
------------- |
| 692 |
-- EID_For -- |
| 693 |
------------- |
| 694 |
|
| 695 |
function EID_For |
| 696 |
(GNAT_Exception : not null GNAT_GCC_Exception_Access) return Exception_Id |
| 697 |
is |
| 698 |
begin |
| 699 |
return GNAT_Exception.Occurrence.Id; |
| 700 |
end EID_For; |
| 701 |
|
| 702 |
---------------------- |
| 703 |
-- Foreign_Data_For -- |
| 704 |
---------------------- |
| 705 |
|
| 706 |
function Foreign_Data_For (E : Exception_Data_Ptr) return Address is |
| 707 |
begin |
| 708 |
return E.Foreign_Data; |
| 709 |
end Foreign_Data_For; |
| 710 |
|
| 711 |
-------------------------- |
| 712 |
-- Is_Handled_By_Others -- |
| 713 |
-------------------------- |
| 714 |
|
| 715 |
function Is_Handled_By_Others (E : Exception_Data_Ptr) return bool is |
| 716 |
begin |
| 717 |
return not bool (E.all.Not_Handled_By_Others); |
| 718 |
end Is_Handled_By_Others; |
| 719 |
|
| 720 |
------------------ |
| 721 |
-- Language_For -- |
| 722 |
------------------ |
| 723 |
|
| 724 |
function Language_For (E : Exception_Data_Ptr) return Character is |
| 725 |
begin |
| 726 |
return E.all.Lang; |
| 727 |
end Language_For; |
| 728 |
|
| 729 |
end Exception_Propagation; |