|
| 1 | +//===--- Exception.cpp - Exception support --------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Swift doesn't support exception handlers, but might call code that uses |
| 14 | +// exceptions, and when they leak out into Swift code, we want to trap them. |
| 15 | +// |
| 16 | +// To that end, we have our own exception personality routine, which we use |
| 17 | +// to trap exceptions and terminate. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#if defined(__ELF__) || defined(__APPLE__) |
| 22 | + |
| 23 | +#include <exception> |
| 24 | + |
| 25 | +#include <cstdio> |
| 26 | + |
| 27 | +#include <unwind.h> |
| 28 | + |
| 29 | +#include "swift/Runtime/Exception.h" |
| 30 | + |
| 31 | +using namespace swift; |
| 32 | + |
| 33 | +extern "C" void __cxa_begin_catch(void *); |
| 34 | + |
| 35 | +SWIFT_RUNTIME_STDLIB_API _Unwind_Reason_Code |
| 36 | +swift_exceptionPersonality(int version, |
| 37 | + _Unwind_Action actions, |
| 38 | + uint64_t exceptionClass, |
| 39 | + struct _Unwind_Exception *exceptionObject, |
| 40 | + struct _Unwind_Context *context) |
| 41 | +{ |
| 42 | + // Handle exceptions by catching them and calling std::terminate(). |
| 43 | + // This, in turn, will trigger the unhandled exception routine in the |
| 44 | + // C++ runtime. |
| 45 | + __cxa_begin_catch(exceptionObject); |
| 46 | + std::terminate(); |
| 47 | + |
| 48 | + return _URC_FATAL_PHASE1_ERROR; |
| 49 | +} |
| 50 | + |
| 51 | +#endif /* defined(__ELF__) || defined(__APPLE__) */ |
0 commit comments