1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
//===--- Errors.cpp - Demangling library error handling ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Provides the demangling library with its own error handling functions.
// This is necessary because it isn't always linked with the runtime, so
// it can't use the runtime's error handling.
//
//===----------------------------------------------------------------------===//
#include "swift/Demangling/Errors.h"
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <io.h>
#endif
#if SWIFT_STDLIB_HAS_ASL
#include <asl.h>
#elif defined(__ANDROID__)
#include <android/log.h>
#endif
#if SWIFT_HAVE_CRASHREPORTERCLIENT
#include <atomic>
#include <malloc/malloc.h>
#include "swift/Runtime/Atomic.h"
#include "CrashReporter.h"
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
// -- Declarations -----------------------------------------------------------
static SWIFT_NORETURN void demangleFatal(uint32_t flags, const char *format,
va_list val);
static void demangleWarn(uint32_t flags, const char *format, va_list val);
static int demangle_vasprintf(char **strp, const char *format, va_list val);
#if SWIFT_HAVE_CRASHREPORTERCLIENT
static int demangle_asprintf(char **strp, const char *format, ...);
#endif
// -- Crash reporter integration ---------------------------------------------
// Report a message to any forthcoming crash log.
static void reportOnCrash(uint32_t flags, const char *message) {
#if SWIFT_HAVE_CRASHREPORTERCLIENT
char *oldMessage = nullptr;
char *newMessage = nullptr;
oldMessage = std::atomic_load_explicit(
(volatile std::atomic<char *> *)&gCRAnnotations.message,
SWIFT_MEMORY_ORDER_CONSUME);
do {
if (newMessage) {
free(newMessage);
newMessage = nullptr;
}
if (oldMessage) {
demangle_asprintf(&newMessage, "%s%s", oldMessage, message);
} else {
newMessage = strdup(message);
}
} while (!std::atomic_compare_exchange_strong_explicit(
(volatile std::atomic<char *> *)&gCRAnnotations.message,
&oldMessage, newMessage,
std::memory_order_release,
SWIFT_MEMORY_ORDER_CONSUME));
if (oldMessage && malloc_size(oldMessage))
free(oldMessage);
#else
// empty
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
}
// -- Utility functions ------------------------------------------------------
// Report a message to system console and stderr.
static void reportNow(uint32_t flags, const char *message) {
#if defined(_WIN32)
#define STDERR_FILENO 2
_write(STDERR_FILENO, message, strlen(message));
#else
fputs(message, stderr);
fflush(stderr);
#endif
#if SWIFT_STDLIB_HAS_ASL
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", message);
#pragma clang diagnostic pop
#elif defined(__ANDROID__) && !defined(__TERMUX__)
__android_log_print(ANDROID_LOG_FATAL, "SwiftDemangle", "%s", message);
#endif
}
/// Report a fatal error to system console, stderr, and crash logs.
/// Does not crash by itself.
static void reportError(uint32_t flags, const char *message) {
reportNow(flags, message);
reportOnCrash(flags, message);
}
/// Not everything has vasprintf()
SWIFT_VFORMAT(2)
static int demangle_vasprintf(char **strp, const char *format, va_list args) {
va_list args_for_len;
va_copy(args_for_len, args);
int len = vsnprintf(nullptr, 0, format, args_for_len);
va_end(args_for_len);
*strp = nullptr;
if (len < 0)
return -1;
size_t bufsiz = len + 1;
char *buffer = reinterpret_cast<char *>(malloc(bufsiz));
if (!buffer)
return -1;
int result = vsnprintf(buffer, bufsiz, format, args);
if (result < 0) {
free(buffer);
return -1;
}
*strp = buffer;
return result;
}
#if SWIFT_HAVE_CRASHREPORTERCLIENT
SWIFT_FORMAT(2,3)
static int demangle_asprintf(char **strp, const char *format, ...) {
va_list val;
va_start(val, format);
int ret = demangle_vasprintf(strp, format, val);
va_end(val);
return ret;
}
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
// -- Implementation ---------------------------------------------------------
SWIFT_VFORMAT(2)
static SWIFT_NORETURN void demangleFatal(uint32_t flags, const char *format,
va_list val) {
char *message;
if (demangle_vasprintf(&message, format, val) < 0) {
reportError(flags, "unable to format fatal error message");
abort();
}
reportError(flags, message);
abort();
}
SWIFT_VFORMAT(2)
static void demangleWarn(uint32_t flags, const char *format, va_list val) {
char *message;
if (demangle_vasprintf(&message, format, val) < 0) {
reportNow(flags, "unable to format warning message");
return;
}
reportNow(flags, message);
free(message);
}
// -- Public API -------------------------------------------------------------
namespace swift {
namespace Demangle {
SWIFT_BEGIN_INLINE_NAMESPACE
SWIFT_FORMAT(2, 3)
SWIFT_NORETURN void fatal(uint32_t flags, const char *format, ...) {
va_list val;
va_start(val, format);
fatalv(flags, format, val);
}
SWIFT_FORMAT(2, 3)
void warn(uint32_t flags, const char *format, ...) {
va_list val;
va_start(val, format);
warnv(flags, format, val);
va_end(val);
}
SWIFT_VFORMAT(2)
SWIFT_NORETURN void fatalv(uint32_t flags, const char *format, va_list val) {
demangleFatal(flags, format, val);
}
SWIFT_VFORMAT(2)
void warnv(uint32_t flags, const char *format, va_list val) {
demangleWarn(flags, format, val);
}
SWIFT_END_INLINE_NAMESPACE
} // end namespace Demangle
} // end namespace swift
|