|
| 1 | +//===--- Errors.cpp - Demangling library error handling ---------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 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 | +// This file is the public API of the demangler library. |
| 14 | +// Tools which use the demangler library (like lldb) must include this - and |
| 15 | +// only this - header file. |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#include "swift/Demangling/Errors.h" |
| 20 | +#include <inttypes.h> |
| 21 | +#include <stdarg.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#if defined(_WIN32) |
| 27 | +#include <io.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +#if SWIFT_STDLIB_HAS_ASL |
| 31 | +#include <asl.h> |
| 32 | +#elif defined(__ANDROID__) |
| 33 | +#include <android/log.h> |
| 34 | +#endif |
| 35 | + |
| 36 | +#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS |
| 37 | +#include "swift/Runtime/Debug.h" |
| 38 | +#endif |
| 39 | + |
| 40 | +#if !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS |
| 41 | + |
| 42 | +// -- Declarations ----------------------------------------------------------- |
| 43 | + |
| 44 | +static SWIFT_NORETURN void demangleFatal(uint32_t flags, const char *format, |
| 45 | + va_list val); |
| 46 | +static void demangleWarn(uint32_t flags, const char *format, va_list val); |
| 47 | + |
| 48 | +// -- Crash reporter integration --------------------------------------------- |
| 49 | + |
| 50 | +#if SWIFT_HAVE_CRASHREPORTERCLIENT |
| 51 | +#include <malloc/malloc.h> |
| 52 | +#include <pthread.h> |
| 53 | + |
| 54 | +// Instead of linking to CrashReporterClient.a (because it complicates the |
| 55 | +// build system), define the only symbol from that static archive ourselves. |
| 56 | +// |
| 57 | +// The layout of this struct is CrashReporter ABI, so there are no ABI concerns |
| 58 | +// here. |
| 59 | +extern "C" { |
| 60 | +SWIFT_LIBRARY_VISIBILITY |
| 61 | +struct crashreporter_annotations_t gCRAnnotations __attribute__(( |
| 62 | + __section__("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = { |
| 63 | + CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0}; |
| 64 | +} |
| 65 | + |
| 66 | +// Report a message to any forthcoming crash log. |
| 67 | +static void reportOnCrash(uint32_t flags, const char *message) { |
| 68 | + // We must use an "unsafe" mutex in this pathway since the normal "safe" |
| 69 | + // mutex calls fatal when an error is detected and fatal ends up |
| 70 | + // calling us. In other words we could get infinite recursion if the |
| 71 | + // mutex errors. |
| 72 | + static pthread_mutex_t crashlogLock = PTHREAD_MUTEX_INITIALIZER; |
| 73 | + |
| 74 | + pthread_mutex_lock(&crashlogLock); |
| 75 | + |
| 76 | + char *oldMessage = (char *)CRGetCrashLogMessage(); |
| 77 | + char *newMessage; |
| 78 | + if (oldMessage) { |
| 79 | + swift_asprintf(&newMessage, "%s%s", oldMessage, message); |
| 80 | + if (malloc_size(oldMessage)) |
| 81 | + free(oldMessage); |
| 82 | + } else { |
| 83 | + newMessage = strdup(message); |
| 84 | + } |
| 85 | + |
| 86 | + CRSetCrashLogMessage(newMessage); |
| 87 | + |
| 88 | + pthread_mutex_unlock(&crashlogLock); |
| 89 | +} |
| 90 | + |
| 91 | +#else |
| 92 | +static void |
| 93 | + |
| 94 | +reportOnCrash(uint32_t flags, const char *message) { |
| 95 | + // empty |
| 96 | +} |
| 97 | + |
| 98 | +#endif // SWIFT_HAVE_CRASHREPORTERCLIENT |
| 99 | + |
| 100 | +// -- Utility functions ------------------------------------------------------ |
| 101 | + |
| 102 | +// Report a message to system console and stderr. |
| 103 | +static void reportNow(uint32_t flags, const char *message) { |
| 104 | +#if defined(_WIN32) |
| 105 | +#define STDERR_FILENO 2 |
| 106 | + _write(STDERR_FILENO, message, strlen(message)); |
| 107 | +#else |
| 108 | + fputs(message, stderr); |
| 109 | + fflush(stderr); |
| 110 | +#endif |
| 111 | +#if SWIFT_STDLIB_HAS_ASL |
| 112 | + asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", message); |
| 113 | +#elif defined(__ANDROID__) |
| 114 | + __android_log_print(ANDROID_LOG_FATAL, "SwiftDemangle", "%s", message); |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
| 118 | +/// Report a fatal error to system console, stderr, and crash logs. |
| 119 | +/// Does not crash by itself. |
| 120 | +static void reportError(uint32_t flags, const char *message) { |
| 121 | + reportNow(flags, message); |
| 122 | + reportOnCrash(flags, message); |
| 123 | +} |
| 124 | + |
| 125 | +/// Not everything has vasprintf() |
| 126 | +SWIFT_VFORMAT(2) |
| 127 | +static int demangle_vasprintf(char **strp, const char *format, va_list args) { |
| 128 | + va_list args_for_len; |
| 129 | + |
| 130 | + va_copy(args_for_len, args); |
| 131 | + int len = vsnprintf(nullptr, 0, format, args_for_len); |
| 132 | + va_end(args_for_len); |
| 133 | + |
| 134 | + *strp = nullptr; |
| 135 | + |
| 136 | + if (len < 0) |
| 137 | + return -1; |
| 138 | + |
| 139 | + size_t bufsiz = len + 1; |
| 140 | + char *buffer = reinterpret_cast<char *>(malloc(bufsiz)); |
| 141 | + if (!buffer) |
| 142 | + return -1; |
| 143 | + |
| 144 | + int result = vsnprintf(buffer, bufsiz, format, args); |
| 145 | + if (result < 0) { |
| 146 | + free(buffer); |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + *strp = buffer; |
| 151 | + return result; |
| 152 | +} |
| 153 | + |
| 154 | +// -- Implementation --------------------------------------------------------- |
| 155 | + |
| 156 | +static SWIFT_NORETURN void demangleFatal(uint32_t flags, const char *format, |
| 157 | + va_list val) { |
| 158 | + char *message; |
| 159 | + |
| 160 | + if (demangle_vasprintf(&message, format, val) < 0) { |
| 161 | + reportError(flags, "unable to format fatal error message"); |
| 162 | + abort(); |
| 163 | + } |
| 164 | + |
| 165 | + reportError(flags, message); |
| 166 | + abort(); |
| 167 | +} |
| 168 | + |
| 169 | +static void demangleWarn(uint32_t flags, const char *format, va_list val) { |
| 170 | + char *message; |
| 171 | + |
| 172 | + if (demangle_vasprintf(&message, format, val) < 0) { |
| 173 | + reportNow(flags, "unable to format warning message"); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + reportNow(flags, message); |
| 178 | + free(message); |
| 179 | +} |
| 180 | + |
| 181 | +#endif // !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS |
| 182 | + |
| 183 | +// -- Public API ------------------------------------------------------------- |
| 184 | + |
| 185 | +namespace swift { |
| 186 | +namespace Demangle { |
| 187 | +SWIFT_BEGIN_INLINE_NAMESPACE |
| 188 | + |
| 189 | +SWIFT_NORETURN void fatal(uint32_t flags, const char *format, ...) { |
| 190 | + va_list val; |
| 191 | + |
| 192 | + va_start(val, format); |
| 193 | + fatalv(flags, format, val); |
| 194 | +} |
| 195 | + |
| 196 | +void warn(uint32_t flags, const char *format, ...) { |
| 197 | + va_list val; |
| 198 | + |
| 199 | + va_start(val, format); |
| 200 | + warnv(flags, format, val); |
| 201 | + va_end(val); |
| 202 | +} |
| 203 | + |
| 204 | +SWIFT_NORETURN void fatalv(uint32_t flags, const char *format, va_list val) { |
| 205 | +#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS |
| 206 | + swift::fatalErrorv(flags, format, val); |
| 207 | +#else |
| 208 | + demangleFatal(flags, format, val); |
| 209 | +#endif |
| 210 | +} |
| 211 | + |
| 212 | +void warnv(uint32_t flags, const char *format, va_list val) { |
| 213 | +#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS |
| 214 | + swift::warningv(flags, format, val); |
| 215 | +#else |
| 216 | + demangleWarn(flags, format, val); |
| 217 | +#endif |
| 218 | +} |
| 219 | + |
| 220 | +SWIFT_END_INLINE_NAMESPACE |
| 221 | +} // end namespace Demangle |
| 222 | +} // end namespace swift |
0 commit comments