]>
Commit | Line | Data |
---|---|---|
b4c522fa | 1 | // GNU D Compiler exception personality routines. |
99dee823 | 2 | // Copyright (C) 2011-2021 Free Software Foundation, Inc. |
b4c522fa IB |
3 | |
4 | // GCC is free software; you can redistribute it and/or modify it under | |
5 | // the terms of the GNU General Public License as published by the Free | |
6 | // Software Foundation; either version 3, or (at your option) any later | |
7 | // version. | |
8 | ||
9 | // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
10 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 | // for more details. | |
13 | ||
14 | // Under Section 7 of GPL version 3, you are granted additional | |
15 | // permissions described in the GCC Runtime Library Exception, version | |
16 | // 3.1, as published by the Free Software Foundation. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License and | |
19 | // a copy of the GCC Runtime Library Exception along with this program; | |
20 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
21 | // <http://www.gnu.org/licenses/>. | |
22 | ||
23 | // This code is based on the libstdc++ exception handling routines. | |
24 | ||
25 | module gcc.deh; | |
26 | ||
27 | import gcc.unwind; | |
28 | import gcc.unwind.pe; | |
29 | import gcc.builtins; | |
30 | import gcc.config; | |
8e84a142 | 31 | import gcc.attributes; |
b4c522fa IB |
32 | |
33 | extern(C) | |
34 | { | |
35 | int _d_isbaseof(ClassInfo, ClassInfo); | |
36 | void _d_createTrace(Object, void*); | |
ed3ec734 | 37 | void _d_print_throwable(Throwable t); |
b4c522fa IB |
38 | } |
39 | ||
40 | /** | |
41 | * Declare all known and handled exception classes. | |
42 | * D exceptions -- "GNUCD\0\0\0". | |
43 | * C++ exceptions -- "GNUCC++\0" | |
44 | * C++ dependent exceptions -- "GNUCC++\x01" | |
45 | */ | |
46 | static if (GNU_ARM_EABI_Unwinder) | |
47 | { | |
48 | enum _Unwind_Exception_Class gdcExceptionClass = "GNUCD\0\0\0"; | |
49 | enum _Unwind_Exception_Class gxxExceptionClass = "GNUCC++\0"; | |
50 | enum _Unwind_Exception_Class gxxDependentExceptionClass = "GNUCC++\x01"; | |
51 | } | |
52 | else | |
53 | { | |
54 | enum _Unwind_Exception_Class gdcExceptionClass = | |
55 | (cast(_Unwind_Exception_Class)'G' << 56) | | |
56 | (cast(_Unwind_Exception_Class)'N' << 48) | | |
57 | (cast(_Unwind_Exception_Class)'U' << 40) | | |
58 | (cast(_Unwind_Exception_Class)'C' << 32) | | |
59 | (cast(_Unwind_Exception_Class)'D' << 24); | |
60 | ||
61 | enum _Unwind_Exception_Class gxxExceptionClass = | |
62 | (cast(_Unwind_Exception_Class)'G' << 56) | | |
63 | (cast(_Unwind_Exception_Class)'N' << 48) | | |
64 | (cast(_Unwind_Exception_Class)'U' << 40) | | |
65 | (cast(_Unwind_Exception_Class)'C' << 32) | | |
66 | (cast(_Unwind_Exception_Class)'C' << 24) | | |
67 | (cast(_Unwind_Exception_Class)'+' << 16) | | |
68 | (cast(_Unwind_Exception_Class)'+' << 8) | | |
69 | (cast(_Unwind_Exception_Class)0 << 0); | |
70 | ||
71 | enum _Unwind_Exception_Class gxxDependentExceptionClass = | |
72 | gxxExceptionClass + 1; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Checks for GDC exception class. | |
77 | */ | |
78 | bool isGdcExceptionClass(_Unwind_Exception_Class c) @nogc | |
79 | { | |
80 | static if (GNU_ARM_EABI_Unwinder) | |
81 | { | |
82 | return c[0] == gdcExceptionClass[0] | |
83 | && c[1] == gdcExceptionClass[1] | |
84 | && c[2] == gdcExceptionClass[2] | |
85 | && c[3] == gdcExceptionClass[3] | |
86 | && c[4] == gdcExceptionClass[4] | |
87 | && c[5] == gdcExceptionClass[5] | |
88 | && c[6] == gdcExceptionClass[6] | |
89 | && c[7] == gdcExceptionClass[7]; | |
90 | } | |
91 | else | |
92 | { | |
93 | return c == gdcExceptionClass; | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Checks for any C++ exception class. | |
99 | */ | |
100 | bool isGxxExceptionClass(_Unwind_Exception_Class c) @nogc | |
101 | { | |
102 | static if (GNU_ARM_EABI_Unwinder) | |
103 | { | |
104 | return c[0] == gxxExceptionClass[0] | |
105 | && c[1] == gxxExceptionClass[1] | |
106 | && c[2] == gxxExceptionClass[2] | |
107 | && c[3] == gxxExceptionClass[3] | |
108 | && c[4] == gxxExceptionClass[4] | |
109 | && c[5] == gxxExceptionClass[5] | |
110 | && c[6] == gxxExceptionClass[6] | |
111 | && (c[7] == gxxExceptionClass[7] | |
112 | || c[7] == gxxDependentExceptionClass[7]); | |
113 | } | |
114 | else | |
115 | { | |
116 | return c == gxxExceptionClass | |
117 | || c == gxxDependentExceptionClass; | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Checks for primary or dependent, but not that it is a C++ exception. | |
123 | */ | |
124 | bool isDependentException(_Unwind_Exception_Class c) @nogc | |
125 | { | |
126 | static if (GNU_ARM_EABI_Unwinder) | |
127 | return (c[7] == '\x01'); | |
128 | else | |
129 | return (c & 1); | |
130 | } | |
131 | ||
132 | /** | |
133 | * A D exception object consists of a header, which is a wrapper | |
134 | * around an unwind object header with additional D specific | |
135 | * information, prefixed by the exception object itself. | |
136 | */ | |
137 | struct ExceptionHeader | |
138 | { | |
139 | // Because of a lack of __aligned__ style attribute, our object | |
140 | // and the unwind object are the first two fields. | |
141 | static if (Throwable.alignof < _Unwind_Exception.alignof) | |
142 | ubyte[_Unwind_Exception.alignof - Throwable.alignof] pad; | |
143 | ||
144 | // The object being thrown. The compiled code expects this to | |
145 | // be immediately before the generic exception header. | |
146 | Throwable object; | |
147 | ||
148 | // The generic exception header. | |
149 | _Unwind_Exception unwindHeader; | |
150 | ||
151 | static assert(unwindHeader.offsetof - object.offsetof == object.sizeof); | |
152 | ||
153 | // Cache handler details between Phase 1 and Phase 2. | |
154 | static if (GNU_ARM_EABI_Unwinder) | |
155 | { | |
156 | // Nothing here yet. | |
157 | } | |
158 | else | |
159 | { | |
160 | // Which catch was found. | |
161 | int handler; | |
162 | ||
163 | // Language Specific Data Area for function enclosing the handler. | |
164 | const(ubyte)* languageSpecificData; | |
165 | ||
166 | // Pointer to catch code. | |
167 | _Unwind_Ptr landingPad; | |
168 | ||
169 | // Canonical Frame Address (CFA) for the enclosing handler. | |
170 | _Unwind_Word canonicalFrameAddress; | |
171 | } | |
172 | ||
173 | // Stack other thrown exceptions in current thread through here. | |
174 | ExceptionHeader* next; | |
175 | ||
176 | // Thread local stack of chained exceptions. | |
177 | static ExceptionHeader* stack; | |
178 | ||
179 | // Pre-allocate storage for 1 instance per thread. | |
180 | // Use calloc/free for multiple exceptions in flight. | |
181 | static ExceptionHeader ehstorage; | |
182 | ||
183 | /** | |
184 | * Allocate and initialize an ExceptionHeader. | |
185 | */ | |
186 | static ExceptionHeader* create(Throwable o) @nogc | |
187 | { | |
188 | auto eh = &ehstorage; | |
189 | ||
190 | // Check exception object in use. | |
191 | if (eh.object) | |
192 | { | |
193 | eh = cast(ExceptionHeader*) __builtin_calloc(ExceptionHeader.sizeof, 1); | |
194 | // Out of memory while throwing - not much else can be done. | |
195 | if (!eh) | |
196 | terminate("out of memory", __LINE__); | |
197 | } | |
198 | eh.object = o; | |
199 | ||
200 | eh.unwindHeader.exception_class = gdcExceptionClass; | |
201 | ||
202 | return eh; | |
203 | } | |
204 | ||
205 | /** | |
206 | * Free ExceptionHeader that was created by create(). | |
207 | */ | |
208 | static void free(ExceptionHeader* eh) @nogc | |
209 | { | |
210 | *eh = ExceptionHeader.init; | |
211 | if (eh != &ehstorage) | |
212 | __builtin_free(eh); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Push this onto stack of chained exceptions. | |
217 | */ | |
218 | void push() @nogc | |
219 | { | |
220 | next = stack; | |
221 | stack = &this; | |
222 | } | |
223 | ||
224 | /** | |
225 | * Pop and return top of chained exception stack. | |
226 | */ | |
227 | static ExceptionHeader* pop() @nogc | |
228 | { | |
229 | auto eh = stack; | |
230 | stack = eh.next; | |
231 | return eh; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Save stage1 handler information in the exception object. | |
236 | */ | |
237 | static void save(_Unwind_Exception* unwindHeader, | |
238 | _Unwind_Word cfa, int handler, | |
239 | const(ubyte)* lsda, _Unwind_Ptr landingPad) @nogc | |
240 | { | |
241 | static if (GNU_ARM_EABI_Unwinder) | |
242 | { | |
243 | unwindHeader.barrier_cache.sp = cfa; | |
244 | unwindHeader.barrier_cache.bitpattern[1] = cast(_uw)handler; | |
245 | unwindHeader.barrier_cache.bitpattern[2] = cast(_uw)lsda; | |
246 | unwindHeader.barrier_cache.bitpattern[3] = cast(_uw)landingPad; | |
247 | } | |
248 | else | |
249 | { | |
250 | ExceptionHeader* eh = toExceptionHeader(unwindHeader); | |
251 | eh.canonicalFrameAddress = cfa; | |
252 | eh.handler = handler; | |
253 | eh.languageSpecificData = lsda; | |
254 | eh.landingPad = landingPad; | |
255 | } | |
256 | } | |
257 | ||
258 | /** | |
259 | * Restore the catch handler data saved during phase1. | |
260 | */ | |
261 | static void restore(_Unwind_Exception* unwindHeader, out int handler, | |
262 | out const(ubyte)* lsda, out _Unwind_Ptr landingPad, | |
263 | out _Unwind_Word cfa) @nogc | |
264 | { | |
265 | static if (GNU_ARM_EABI_Unwinder) | |
266 | { | |
267 | cfa = unwindHeader.barrier_cache.sp; | |
268 | handler = cast(int)unwindHeader.barrier_cache.bitpattern[1]; | |
269 | lsda = cast(ubyte*)unwindHeader.barrier_cache.bitpattern[2]; | |
270 | landingPad = cast(_Unwind_Ptr)unwindHeader.barrier_cache.bitpattern[3]; | |
271 | } | |
272 | else | |
273 | { | |
274 | ExceptionHeader* eh = toExceptionHeader(unwindHeader); | |
275 | cfa = eh.canonicalFrameAddress; | |
276 | handler = eh.handler; | |
277 | lsda = eh.languageSpecificData; | |
278 | landingPad = cast(_Unwind_Ptr)eh.landingPad; | |
279 | } | |
280 | } | |
281 | ||
b4c522fa IB |
282 | /** |
283 | * Convert from pointer to unwindHeader to pointer to ExceptionHeader | |
284 | * that it is embedded inside of. | |
285 | */ | |
286 | static ExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc | |
287 | { | |
288 | return cast(ExceptionHeader*)(cast(void*)exc - ExceptionHeader.unwindHeader.offsetof); | |
289 | } | |
290 | } | |
291 | ||
292 | /** | |
293 | * Map to C++ std::type_info's virtual functions from D, | |
294 | * being careful to not require linking with libstdc++. | |
295 | * So it is given a different name. | |
296 | */ | |
297 | extern(C++) interface CxxTypeInfo | |
298 | { | |
299 | void dtor1(); | |
300 | void dtor2(); | |
301 | bool __is_pointer_p() const; | |
302 | bool __is_function_p() const; | |
303 | bool __do_catch(const CxxTypeInfo, void**, uint) const; | |
304 | bool __do_upcast(const void*, void**) const; | |
305 | } | |
306 | ||
307 | /** | |
308 | * Structure of a C++ exception, represented as a C structure. | |
309 | * See unwind-cxx.h for the full definition. | |
310 | */ | |
311 | struct CxaExceptionHeader | |
312 | { | |
313 | union | |
314 | { | |
315 | CxxTypeInfo exceptionType; | |
316 | void* primaryException; | |
317 | } | |
318 | void function(void*) exceptionDestructor; | |
319 | void function() unexpectedHandler; | |
320 | void function() terminateHandler; | |
321 | CxaExceptionHeader* nextException; | |
322 | int handlerCount; | |
323 | ||
324 | static if (GNU_ARM_EABI_Unwinder) | |
325 | { | |
326 | CxaExceptionHeader* nextPropagatingException; | |
327 | int propagationCount; | |
328 | } | |
329 | else | |
330 | { | |
331 | int handlerSwitchValue; | |
332 | const(ubyte)* actionRecord; | |
333 | const(ubyte)* languageSpecificData; | |
334 | _Unwind_Ptr catchTemp; | |
335 | void* adjustedPtr; | |
336 | } | |
337 | ||
338 | _Unwind_Exception unwindHeader; | |
339 | ||
340 | /** | |
341 | * There's no saving between phases, so only cache pointer. | |
342 | * __cxa_begin_catch expects this to be set. | |
343 | */ | |
344 | static void save(_Unwind_Exception* unwindHeader, void* thrownPtr) @nogc | |
345 | { | |
346 | static if (GNU_ARM_EABI_Unwinder) | |
347 | unwindHeader.barrier_cache.bitpattern[0] = cast(_uw) thrownPtr; | |
348 | else | |
349 | { | |
350 | auto eh = toExceptionHeader(unwindHeader); | |
351 | eh.adjustedPtr = thrownPtr; | |
352 | } | |
353 | } | |
354 | ||
355 | /** | |
356 | * Get pointer to the thrown object if the thrown object type behind the | |
357 | * exception is implicitly convertible to the catch type. | |
358 | */ | |
359 | static void* getAdjustedPtr(_Unwind_Exception* exc, CxxTypeInfo catchType) | |
360 | { | |
361 | void* thrownPtr; | |
362 | ||
363 | // A dependent C++ exceptions is just a wrapper around the unwind header. | |
364 | // A primary C++ exception has the thrown object located immediately after it. | |
365 | if (isDependentException(exc.exception_class)) | |
366 | thrownPtr = toExceptionHeader(exc).primaryException; | |
367 | else | |
368 | thrownPtr = cast(void*)(exc + 1); | |
369 | ||
370 | // Pointer types need to adjust the actual pointer, not the pointer that is | |
371 | // the exception object. This also has the effect of passing pointer types | |
372 | // "by value" through the __cxa_begin_catch return value. | |
373 | const throw_type = (cast(CxaExceptionHeader*)thrownPtr - 1).exceptionType; | |
374 | ||
375 | if (throw_type.__is_pointer_p()) | |
376 | thrownPtr = *cast(void**)thrownPtr; | |
377 | ||
378 | // Pointer adjustment may be necessary due to multiple inheritance | |
379 | if (catchType is throw_type | |
380 | || catchType.__do_catch(throw_type, &thrownPtr, 1)) | |
381 | return thrownPtr; | |
382 | ||
383 | return null; | |
384 | } | |
385 | ||
386 | /** | |
387 | * Convert from pointer to unwindHeader to pointer to CxaExceptionHeader | |
388 | * that it is embedded inside of. | |
389 | */ | |
390 | static CxaExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc | |
391 | { | |
392 | return cast(CxaExceptionHeader*)(exc + 1) - 1; | |
393 | } | |
394 | } | |
395 | ||
396 | /** | |
397 | * Called if exception handling must be abandoned for any reason. | |
398 | */ | |
399 | private void terminate(string msg, uint line) @nogc | |
400 | { | |
401 | import core.stdc.stdio; | |
402 | import core.stdc.stdlib; | |
403 | ||
404 | static bool terminating; | |
405 | if (terminating) | |
406 | { | |
407 | fputs("terminate called recursively\n", stderr); | |
408 | abort(); | |
409 | } | |
410 | terminating = true; | |
411 | ||
412 | fprintf(stderr, "gcc.deh(%u): %.*s\n", line, cast(int)msg.length, msg.ptr); | |
413 | ||
414 | abort(); | |
415 | } | |
416 | ||
417 | /** | |
418 | * Called when fibers switch contexts. | |
419 | */ | |
420 | extern(C) void* _d_eh_swapContext(void* newContext) nothrow @nogc | |
421 | { | |
422 | auto old = ExceptionHeader.stack; | |
423 | ExceptionHeader.stack = cast(ExceptionHeader*)newContext; | |
424 | return old; | |
425 | } | |
426 | ||
427 | /** | |
428 | * Called before starting a catch. Returns the exception object. | |
429 | */ | |
430 | extern(C) void* __gdc_begin_catch(_Unwind_Exception* unwindHeader) | |
431 | { | |
432 | ExceptionHeader* header = ExceptionHeader.toExceptionHeader(unwindHeader); | |
433 | ||
434 | void* objectp = cast(void*)header.object; | |
435 | ||
436 | // Something went wrong when stacking up chained headers... | |
437 | if (header != ExceptionHeader.pop()) | |
438 | terminate("catch error", __LINE__); | |
439 | ||
440 | // Handling for this exception is complete. | |
441 | _Unwind_DeleteException(&header.unwindHeader); | |
442 | ||
443 | return objectp; | |
444 | } | |
445 | ||
446 | /** | |
447 | * Perform a throw, D style. Throw will unwind through this call, | |
448 | * so there better not be any handlers or exception thrown here. | |
449 | */ | |
450 | extern(C) void _d_throw(Throwable object) | |
451 | { | |
452 | // If possible, avoid always allocating new memory for exception headers. | |
453 | ExceptionHeader *eh = ExceptionHeader.create(object); | |
454 | ||
455 | // Add to thrown exception stack. | |
456 | eh.push(); | |
457 | ||
458 | // Called by unwinder when exception object needs destruction by other than our code. | |
459 | extern(C) void exception_cleanup(_Unwind_Reason_Code code, _Unwind_Exception* exc) | |
460 | { | |
461 | // If we haven't been caught by a foreign handler, then this is | |
462 | // some sort of unwind error. In that case just die immediately. | |
463 | // _Unwind_DeleteException in the HP-UX IA64 libunwind library | |
464 | // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT | |
465 | // like the GCC _Unwind_DeleteException function does. | |
466 | if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON) | |
467 | terminate("uncaught exception", __LINE__); | |
468 | ||
469 | auto eh = ExceptionHeader.toExceptionHeader(exc); | |
470 | ExceptionHeader.free(eh); | |
471 | } | |
472 | ||
473 | eh.unwindHeader.exception_cleanup = &exception_cleanup; | |
474 | ||
475 | // Runtime now expects us to do this first before unwinding. | |
476 | _d_createTrace(eh.object, null); | |
477 | ||
478 | // We're happy with setjmp/longjmp exceptions or region-based | |
479 | // exception handlers: entry points are provided here for both. | |
480 | _Unwind_Reason_Code r = void; | |
481 | ||
482 | version (GNU_SjLj_Exceptions) | |
483 | r = _Unwind_SjLj_RaiseException(&eh.unwindHeader); | |
484 | else | |
485 | r = _Unwind_RaiseException(&eh.unwindHeader); | |
486 | ||
487 | // If code == _URC_END_OF_STACK, then we reached top of stack without finding | |
488 | // a handler for the exception. Since each thread is run in a try/catch, | |
489 | // this oughtn't happen. If code is something else, we encountered some sort | |
490 | // of heinous lossage from which we could not recover. As is the way of such | |
491 | // things, almost certainly we will have crashed before now, rather than | |
492 | // actually being able to diagnose the problem. | |
493 | if (r == _URC_END_OF_STACK) | |
ed3ec734 IB |
494 | { |
495 | __gdc_begin_catch(&eh.unwindHeader); | |
496 | _d_print_throwable(object); | |
b4c522fa | 497 | terminate("uncaught exception", __LINE__); |
ed3ec734 | 498 | } |
b4c522fa IB |
499 | |
500 | terminate("unwind error", __LINE__); | |
501 | } | |
502 | ||
48528842 RR |
503 | static if (GNU_ARM_EABI_Unwinder) |
504 | { | |
505 | enum personality_fn_attributes = attribute("target", ("general-regs-only")); | |
506 | } | |
507 | else | |
508 | { | |
509 | enum personality_fn_attributes = ""; | |
510 | } | |
b4c522fa IB |
511 | |
512 | /** | |
513 | * Read and extract information from the LSDA (.gcc_except_table section). | |
514 | */ | |
48528842 | 515 | @personality_fn_attributes |
b4c522fa IB |
516 | _Unwind_Reason_Code scanLSDA(const(ubyte)* lsda, _Unwind_Exception_Class exceptionClass, |
517 | _Unwind_Action actions, _Unwind_Exception* unwindHeader, | |
518 | _Unwind_Context* context, _Unwind_Word cfa, | |
519 | out _Unwind_Ptr landingPad, out int handler) | |
520 | { | |
521 | // If no LSDA, then there are no handlers or cleanups. | |
522 | if (lsda is null) | |
523 | return CONTINUE_UNWINDING(unwindHeader, context); | |
524 | ||
525 | // Parse the LSDA header | |
526 | auto p = lsda; | |
527 | ||
528 | auto Start = (context ? _Unwind_GetRegionStart(context) : 0); | |
529 | ||
530 | // Find @LPStart, the base to which landing pad offsets are relative. | |
531 | ubyte LPStartEncoding = *p++; | |
532 | _Unwind_Ptr LPStart = 0; | |
533 | ||
534 | if (LPStartEncoding != DW_EH_PE_omit) | |
30b11d8d | 535 | LPStart = read_encoded_value(context, LPStartEncoding, p); |
b4c522fa IB |
536 | else |
537 | LPStart = Start; | |
538 | ||
539 | // Find @TType, the base of the handler and exception spec type data. | |
540 | ubyte TTypeEncoding = *p++; | |
541 | const(ubyte)* TType = null; | |
542 | ||
543 | if (TTypeEncoding != DW_EH_PE_omit) | |
544 | { | |
545 | static if (__traits(compiles, _TTYPE_ENCODING)) | |
546 | { | |
547 | // Older ARM EABI toolchains set this value incorrectly, so use a | |
548 | // hardcoded OS-specific format. | |
549 | TTypeEncoding = _TTYPE_ENCODING; | |
550 | } | |
30b11d8d | 551 | auto TTbase = read_uleb128(p); |
b4c522fa IB |
552 | TType = p + TTbase; |
553 | } | |
554 | ||
555 | // The encoding and length of the call-site table; the action table | |
556 | // immediately follows. | |
557 | ubyte CSEncoding = *p++; | |
30b11d8d | 558 | auto CSTableSize = read_uleb128(p); |
b4c522fa IB |
559 | const(ubyte)* actionTable = p + CSTableSize; |
560 | ||
561 | auto TTypeBase = base_of_encoded_value(TTypeEncoding, context); | |
562 | ||
563 | // Get instruction pointer (ip) at start of instruction that threw. | |
564 | version (CRuntime_Glibc) | |
565 | { | |
566 | int ip_before_insn; | |
567 | auto ip = _Unwind_GetIPInfo(context, &ip_before_insn); | |
568 | if (!ip_before_insn) | |
569 | --ip; | |
570 | } | |
571 | else | |
572 | { | |
573 | auto ip = _Unwind_GetIP(context); | |
574 | --ip; | |
575 | } | |
576 | ||
577 | bool saw_cleanup = false; | |
578 | bool saw_handler = false; | |
579 | const(ubyte)* actionRecord = null; | |
580 | ||
581 | version (GNU_SjLj_Exceptions) | |
582 | { | |
583 | // The given "IP" is an index into the call-site table, with two | |
584 | // exceptions -- -1 means no-action, and 0 means terminate. | |
585 | // But since we're using uleb128 values, we've not got random | |
586 | // access to the array. | |
587 | if (cast(int) ip <= 0) | |
588 | { | |
589 | return _URC_CONTINUE_UNWIND; | |
590 | } | |
591 | else | |
592 | { | |
593 | _uleb128_t CSLandingPad, CSAction; | |
594 | do | |
595 | { | |
30b11d8d IB |
596 | CSLandingPad = read_uleb128(p); |
597 | CSAction = read_uleb128(p); | |
b4c522fa IB |
598 | } |
599 | while (--ip); | |
600 | ||
601 | // Can never have null landing pad for sjlj -- that would have | |
602 | // been indicated by a -1 call site index. | |
603 | landingPad = CSLandingPad + 1; | |
604 | if (CSAction) | |
605 | actionRecord = actionTable + CSAction - 1; | |
606 | } | |
607 | } | |
608 | else | |
609 | { | |
610 | // Search the call-site table for the action associated with this IP. | |
611 | while (p < actionTable) | |
612 | { | |
613 | // Note that all call-site encodings are "absolute" displacements. | |
30b11d8d IB |
614 | auto CSStart = read_encoded_value(null, CSEncoding, p); |
615 | auto CSLen = read_encoded_value(null, CSEncoding, p); | |
616 | auto CSLandingPad = read_encoded_value(null, CSEncoding, p); | |
617 | auto CSAction = read_uleb128(p); | |
b4c522fa IB |
618 | |
619 | // The table is sorted, so if we've passed the ip, stop. | |
620 | if (ip < Start + CSStart) | |
621 | p = actionTable; | |
622 | else if (ip < Start + CSStart + CSLen) | |
623 | { | |
624 | if (CSLandingPad) | |
625 | landingPad = LPStart + CSLandingPad; | |
626 | if (CSAction) | |
627 | actionRecord = actionTable + CSAction - 1; | |
628 | break; | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | if (landingPad == 0) | |
634 | { | |
635 | // IP is present, but has a null landing pad. | |
636 | // No cleanups or handlers to be run. | |
637 | } | |
638 | else if (actionRecord is null) | |
639 | { | |
640 | // If ip is present, has a non-null landing pad, and a null | |
641 | // action table offset, then there are only cleanups present. | |
642 | // Cleanups use a zero switch value, as set above. | |
643 | saw_cleanup = true; | |
644 | } | |
645 | else | |
646 | { | |
647 | // Otherwise we have a catch handler or exception specification. | |
648 | handler = actionTableLookup(actions, unwindHeader, actionRecord, | |
8088a33d | 649 | lsda, exceptionClass, TTypeBase, |
b4c522fa IB |
650 | TType, TTypeEncoding, |
651 | saw_handler, saw_cleanup); | |
652 | } | |
653 | ||
654 | // IP is not in table. No associated cleanups. | |
655 | if (!saw_handler && !saw_cleanup) | |
656 | return CONTINUE_UNWINDING(unwindHeader, context); | |
657 | ||
658 | if (actions & _UA_SEARCH_PHASE) | |
659 | { | |
660 | if (!saw_handler) | |
661 | return CONTINUE_UNWINDING(unwindHeader, context); | |
662 | ||
663 | // For domestic exceptions, we cache data from phase 1 for phase 2. | |
664 | if (isGdcExceptionClass(exceptionClass)) | |
665 | ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad); | |
666 | ||
667 | return _URC_HANDLER_FOUND; | |
668 | } | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | /** | |
674 | * Look up and return the handler index of the classType in Action Table. | |
675 | */ | |
676 | int actionTableLookup(_Unwind_Action actions, _Unwind_Exception* unwindHeader, | |
8088a33d IB |
677 | const(ubyte)* actionRecord, const(ubyte)* lsda, |
678 | _Unwind_Exception_Class exceptionClass, | |
b4c522fa IB |
679 | _Unwind_Ptr TTypeBase, const(ubyte)* TType, |
680 | ubyte TTypeEncoding, | |
681 | out bool saw_handler, out bool saw_cleanup) | |
682 | { | |
683 | ClassInfo thrownType; | |
684 | if (isGdcExceptionClass(exceptionClass)) | |
685 | { | |
8088a33d | 686 | thrownType = getClassInfo(unwindHeader, lsda); |
b4c522fa IB |
687 | } |
688 | ||
689 | while (1) | |
690 | { | |
691 | auto ap = actionRecord; | |
30b11d8d | 692 | auto ARFilter = read_sleb128(ap); |
b4c522fa | 693 | auto apn = ap; |
30b11d8d | 694 | auto ARDisp = read_sleb128(ap); |
b4c522fa IB |
695 | |
696 | if (ARFilter == 0) | |
697 | { | |
698 | // Zero filter values are cleanups. | |
699 | saw_cleanup = true; | |
700 | } | |
701 | else if (actions & _UA_FORCE_UNWIND) | |
702 | { | |
703 | // During forced unwinding, we only run cleanups. | |
704 | } | |
705 | else if (ARFilter > 0) | |
706 | { | |
707 | // Positive filter values are handlers. | |
708 | auto encodedSize = size_of_encoded_value(TTypeEncoding); | |
709 | ||
710 | // ARFilter is the negative index from TType, which is where | |
711 | // the ClassInfo is stored. | |
712 | const(ubyte)* tp = TType - ARFilter * encodedSize; | |
713 | ||
30b11d8d | 714 | auto entry = read_encoded_value_with_base(TTypeEncoding, TTypeBase, tp); |
b4c522fa IB |
715 | ClassInfo ci = cast(ClassInfo)cast(void*)(entry); |
716 | ||
717 | // D does not have catch-all handlers, and so the following | |
718 | // assumes that we will never handle a null value. | |
719 | assert(ci !is null); | |
720 | ||
721 | if (ci.classinfo is __cpp_type_info_ptr.classinfo | |
722 | && isGxxExceptionClass(exceptionClass)) | |
723 | { | |
724 | // catchType is the catch clause type_info. | |
725 | auto catchType = cast(CxxTypeInfo)((cast(__cpp_type_info_ptr)cast(void*)ci).ptr); | |
726 | auto thrownPtr = CxaExceptionHeader.getAdjustedPtr(unwindHeader, catchType); | |
727 | ||
728 | if (thrownPtr !is null) | |
729 | { | |
730 | if (actions & _UA_SEARCH_PHASE) | |
731 | CxaExceptionHeader.save(unwindHeader, thrownPtr); | |
732 | saw_handler = true; | |
733 | return cast(int)ARFilter; | |
734 | } | |
735 | } | |
736 | else if (isGdcExceptionClass(exceptionClass) | |
737 | && _d_isbaseof(thrownType, ci)) | |
738 | { | |
739 | saw_handler = true; | |
740 | return cast(int)ARFilter; | |
741 | } | |
742 | else | |
743 | { | |
744 | // ??? What to do about other GNU language exceptions. | |
745 | } | |
746 | } | |
747 | else | |
748 | { | |
749 | // Negative filter values are exception specifications, | |
750 | // which D does not use. | |
751 | break; | |
752 | } | |
753 | ||
754 | if (ARDisp == 0) | |
755 | break; | |
756 | actionRecord = apn + ARDisp; | |
757 | } | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
8088a33d IB |
762 | /** |
763 | * Look at the chain of inflight exceptions and pick the class type that'll | |
764 | * be looked for in catch clauses. | |
765 | */ | |
766 | ClassInfo getClassInfo(_Unwind_Exception* unwindHeader, | |
767 | const(ubyte)* currentLsd) @nogc | |
768 | { | |
769 | ExceptionHeader* eh = ExceptionHeader.toExceptionHeader(unwindHeader); | |
770 | // The first thrown Exception at the top of the stack takes precedence | |
771 | // over others that are inflight, unless an Error was thrown, in which | |
772 | // case, we search for error handlers instead. | |
773 | Throwable ehobject = eh.object; | |
774 | for (ExceptionHeader* ehn = eh.next; ehn; ehn = ehn.next) | |
775 | { | |
776 | const(ubyte)* nextLsd = void; | |
777 | _Unwind_Ptr nextLandingPad = void; | |
778 | _Unwind_Word nextCfa = void; | |
779 | int nextHandler = void; | |
780 | ||
781 | ExceptionHeader.restore(&ehn.unwindHeader, nextHandler, nextLsd, nextLandingPad, nextCfa); | |
782 | ||
783 | // Don't combine when the exceptions are from different functions. | |
784 | if (currentLsd != nextLsd) | |
785 | break; | |
786 | ||
787 | Error e = cast(Error)ehobject; | |
788 | if (e is null || (cast(Error)ehn.object) !is null) | |
789 | { | |
790 | currentLsd = nextLsd; | |
791 | ehobject = ehn.object; | |
792 | } | |
793 | } | |
794 | return ehobject.classinfo; | |
795 | } | |
796 | ||
b4c522fa IB |
797 | /** |
798 | * Called when the personality function has found neither a cleanup or handler. | |
799 | * To support ARM EABI personality routines, that must also unwind the stack. | |
800 | */ | |
48528842 | 801 | @personality_fn_attributes |
b4c522fa IB |
802 | _Unwind_Reason_Code CONTINUE_UNWINDING(_Unwind_Exception* unwindHeader, _Unwind_Context* context) |
803 | { | |
804 | static if (GNU_ARM_EABI_Unwinder) | |
805 | { | |
806 | if (__gnu_unwind_frame(unwindHeader, context) != _URC_OK) | |
807 | return _URC_FAILURE; | |
808 | } | |
809 | return _URC_CONTINUE_UNWIND; | |
810 | } | |
811 | ||
812 | /** | |
813 | * Using a different personality function name causes link failures | |
814 | * when trying to mix code using different exception handling models. | |
815 | */ | |
816 | version (GNU_SEH_Exceptions) | |
817 | { | |
818 | enum PERSONALITY_FUNCTION = "__gdc_personality_imp"; | |
819 | ||
820 | extern(C) EXCEPTION_DISPOSITION __gdc_personality_seh0(void* ms_exc, void* this_frame, | |
821 | void* ms_orig_context, void* ms_disp) | |
822 | { | |
823 | return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, | |
b66e72b4 | 824 | ms_disp, &gdc_personality); |
b4c522fa IB |
825 | } |
826 | } | |
827 | else version (GNU_SjLj_Exceptions) | |
828 | { | |
829 | enum PERSONALITY_FUNCTION = "__gdc_personality_sj0"; | |
830 | ||
831 | private int __builtin_eh_return_data_regno(int x) { return x; } | |
832 | } | |
833 | else | |
834 | { | |
835 | enum PERSONALITY_FUNCTION = "__gdc_personality_v0"; | |
836 | } | |
837 | ||
838 | /** | |
839 | * The "personality" function, specific to each language. | |
840 | */ | |
841 | static if (GNU_ARM_EABI_Unwinder) | |
842 | { | |
843 | pragma(mangle, PERSONALITY_FUNCTION) | |
48528842 | 844 | @personality_fn_attributes |
b4c522fa IB |
845 | extern(C) _Unwind_Reason_Code gdc_personality(_Unwind_State state, |
846 | _Unwind_Exception* unwindHeader, | |
847 | _Unwind_Context* context) | |
848 | { | |
849 | _Unwind_Action actions; | |
850 | ||
851 | switch (state & _US_ACTION_MASK) | |
852 | { | |
853 | case _US_VIRTUAL_UNWIND_FRAME: | |
854 | // If the unwind state pattern is (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND) | |
855 | // then we don't need to search for any handler as it is not a real exception. | |
856 | // Just unwind the stack. | |
857 | if (state & _US_FORCE_UNWIND) | |
858 | return CONTINUE_UNWINDING(unwindHeader, context); | |
859 | actions = _UA_SEARCH_PHASE; | |
860 | break; | |
861 | ||
862 | case _US_UNWIND_FRAME_STARTING: | |
863 | actions = _UA_CLEANUP_PHASE; | |
864 | if (!(state & _US_FORCE_UNWIND) | |
865 | && unwindHeader.barrier_cache.sp == _Unwind_GetGR(context, UNWIND_STACK_REG)) | |
866 | actions |= _UA_HANDLER_FRAME; | |
867 | break; | |
868 | ||
869 | case _US_UNWIND_FRAME_RESUME: | |
870 | return CONTINUE_UNWINDING(unwindHeader, context); | |
871 | ||
872 | default: | |
873 | terminate("unwind error", __LINE__); | |
874 | } | |
875 | actions |= state & _US_FORCE_UNWIND; | |
876 | ||
877 | // The dwarf unwinder assumes the context structure holds things like | |
878 | // the function and LSDA pointers. The ARM implementation caches these | |
879 | // in the exception header (UCB). To avoid rewriting everything we make | |
880 | // the virtual IP register point at the UCB. | |
881 | _Unwind_SetGR(context, UNWIND_POINTER_REG, cast(_Unwind_Ptr)unwindHeader); | |
882 | ||
883 | return __gdc_personality(actions, unwindHeader.exception_class, | |
884 | unwindHeader, context); | |
885 | } | |
886 | } | |
887 | else | |
888 | { | |
889 | pragma(mangle, PERSONALITY_FUNCTION) | |
890 | extern(C) _Unwind_Reason_Code gdc_personality(int iversion, | |
891 | _Unwind_Action actions, | |
892 | _Unwind_Exception_Class exceptionClass, | |
893 | _Unwind_Exception* unwindHeader, | |
894 | _Unwind_Context* context) | |
895 | { | |
896 | // Interface version check. | |
897 | if (iversion != 1) | |
898 | return _URC_FATAL_PHASE1_ERROR; | |
899 | ||
900 | return __gdc_personality(actions, exceptionClass, unwindHeader, context); | |
901 | } | |
902 | } | |
903 | ||
48528842 | 904 | @personality_fn_attributes |
b4c522fa IB |
905 | private _Unwind_Reason_Code __gdc_personality(_Unwind_Action actions, |
906 | _Unwind_Exception_Class exceptionClass, | |
907 | _Unwind_Exception* unwindHeader, | |
908 | _Unwind_Context* context) | |
909 | { | |
910 | const(ubyte)* lsda; | |
911 | _Unwind_Ptr landingPad; | |
912 | _Unwind_Word cfa; | |
913 | int handler; | |
914 | ||
915 | // Shortcut for phase 2 found handler for domestic exception. | |
916 | if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME) | |
917 | && isGdcExceptionClass(exceptionClass)) | |
918 | { | |
919 | ExceptionHeader.restore(unwindHeader, handler, lsda, landingPad, cfa); | |
920 | // Shouldn't have cached a null landing pad in phase 1. | |
921 | if (landingPad == 0) | |
922 | terminate("unwind error", __LINE__); | |
923 | } | |
924 | else | |
925 | { | |
926 | lsda = cast(ubyte*)_Unwind_GetLanguageSpecificData(context); | |
927 | ||
928 | static if (GNU_ARM_EABI_Unwinder) | |
929 | cfa = _Unwind_GetGR(context, UNWIND_STACK_REG); | |
930 | else | |
931 | cfa = _Unwind_GetCFA(context); | |
932 | ||
933 | auto result = scanLSDA(lsda, exceptionClass, actions, unwindHeader, | |
934 | context, cfa, landingPad, handler); | |
935 | ||
936 | // Positive on handler found in phase 1, continue unwinding, or failure. | |
937 | if (result) | |
938 | return result; | |
939 | } | |
940 | ||
941 | // Unexpected negative handler, call terminate directly. | |
942 | if (handler < 0) | |
943 | terminate("unwind error", __LINE__); | |
944 | ||
945 | // We can't use any of the deh routines with foreign exceptions, | |
946 | // because they all expect unwindHeader to be an ExceptionHeader. | |
947 | if (isGdcExceptionClass(exceptionClass)) | |
948 | { | |
949 | // If there are any in-flight exceptions being thrown, chain our | |
950 | // current object onto the end of the prevous object. | |
951 | ExceptionHeader* eh = ExceptionHeader.toExceptionHeader(unwindHeader); | |
952 | auto currentLsd = lsda; | |
b4c522fa IB |
953 | bool bypassed = false; |
954 | ||
955 | while (eh.next) | |
956 | { | |
957 | ExceptionHeader* ehn = eh.next; | |
8088a33d IB |
958 | const(ubyte)* nextLsd = void; |
959 | _Unwind_Ptr nextLandingPad = void; | |
960 | _Unwind_Word nextCfa = void; | |
961 | int nextHandler = void; | |
b4c522fa IB |
962 | |
963 | ExceptionHeader.restore(&ehn.unwindHeader, nextHandler, nextLsd, nextLandingPad, nextCfa); | |
964 | ||
965 | Error e = cast(Error)eh.object; | |
966 | if (e !is null && !cast(Error)ehn.object) | |
967 | { | |
968 | // We found an Error, bypass the exception chain. | |
969 | currentLsd = nextLsd; | |
b4c522fa IB |
970 | eh = ehn; |
971 | bypassed = true; | |
972 | continue; | |
973 | } | |
974 | ||
975 | // Don't combine when the exceptions are from different functions. | |
8088a33d | 976 | if (currentLsd != nextLsd) |
b4c522fa IB |
977 | break; |
978 | ||
979 | // Add our object onto the end of the existing chain. | |
980 | Throwable n = ehn.object; | |
981 | while (n.next) | |
982 | n = n.next; | |
983 | n.next = eh.object; | |
984 | ||
985 | // Replace our exception object with in-flight one | |
986 | eh.object = ehn.object; | |
987 | if (nextHandler != handler && !bypassed) | |
988 | { | |
989 | handler = nextHandler; | |
990 | ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad); | |
991 | } | |
992 | ||
993 | // Exceptions chained, can now throw away the previous header. | |
994 | eh.next = ehn.next; | |
995 | _Unwind_DeleteException(&ehn.unwindHeader); | |
996 | } | |
997 | ||
998 | if (bypassed) | |
999 | { | |
1000 | eh = ExceptionHeader.toExceptionHeader(unwindHeader); | |
1001 | Error e = cast(Error)eh.object; | |
1002 | auto ehn = eh.next; | |
1003 | e.bypassedException = ehn.object; | |
1004 | eh.next = ehn.next; | |
1005 | _Unwind_DeleteException(&ehn.unwindHeader); | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | // Set up registers and jump to cleanup or handler. | |
1010 | // For targets with pointers smaller than the word size, we must extend the | |
1011 | // pointer, and this extension is target dependent. | |
1012 | _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), | |
1013 | cast(_Unwind_Ptr)unwindHeader); | |
1014 | _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), handler); | |
1015 | _Unwind_SetIP(context, landingPad); | |
1016 | ||
1017 | return _URC_INSTALL_CONTEXT; | |
1018 | } |