Skip to content

Commit 1df2103

Browse files
committed
Runtime: add a non-Darwin error message storage
This introduces a non-Darwin (non-CrashReporter) storage for error messages to allow extraction for crash reporting. This is initially meant to be used on Windows, though it is generic enough to be used on any platform.
1 parent a6aa2dc commit 1df2103

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

include/swift/Runtime/Debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ void swift_reportError(uint32_t flags, const char *message);
127127
SWIFT_RUNTIME_EXPORT
128128
void swift_reportWarning(uint32_t flags, const char *message);
129129

130+
SWIFT_RUNTIME_EXPORT
131+
const char **swift_getFatalErrorMessageBuffer();
132+
130133
// Halt due to an overflow in swift_retain().
131134
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
132135
void swift_abortRetainOverflow();

stdlib/public/runtime/Errors.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "ImageInspection.h"
3636
#include "swift/Demangling/Demangle.h"
37+
#include "swift/Runtime/Atomic.h"
3738
#include "swift/Runtime/Debug.h"
3839
#include "swift/Runtime/Portability.h"
3940
#include "swift/Runtime/Win32.h"
@@ -66,8 +67,8 @@
6667
#ifdef SWIFT_HAVE_CRASHREPORTERCLIENT
6768
#include <atomic>
6869
#include <malloc/malloc.h>
69-
70-
#include "swift/Runtime/Atomic.h"
70+
#else
71+
static char *kFatalErrorMessage;
7172
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
7273

7374
#include "BacktracePrivate.h"
@@ -297,7 +298,23 @@ reportOnCrash(uint32_t flags, const char *message)
297298
std::memory_order_release,
298299
SWIFT_MEMORY_ORDER_CONSUME));
299300
#else
300-
// empty
301+
char *previous = nullptr, *current = nullptr;
302+
previous =
303+
std::atomic_load_explicit((const char * _Atomic *)&kFatalErrorMessage,
304+
SWIFT_MEMORY_ORDER_CONSUME);
305+
306+
do {
307+
free(current);
308+
current = nullptr;
309+
310+
if (previous)
311+
swift_asprintf(&current, "%s%s", current, message);
312+
else
313+
current = strdup(message);
314+
} while (!std::atomic_compare_exchange_strong_explicit((const char * _Atomic *)&kFatalErrorMessage,
315+
&previous, current,
316+
std::memory_order_release,
317+
SWIFT_MEMORY_ORDER_CONSUME));
301318
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
302319
}
303320

@@ -421,6 +438,10 @@ void swift::swift_reportWarning(uint32_t flags, const char *message) {
421438
warning(flags, "%s", message);
422439
}
423440

441+
const char **swift::swift_getFatalErrorMessageBuffer() {
442+
return &kFatalErrorMessage;
443+
}
444+
424445
// Crash when a deleted method is called by accident.
425446
SWIFT_RUNTIME_EXPORT SWIFT_NORETURN void swift_deletedMethodError() {
426447
swift::fatalError(/* flags = */ 0,

0 commit comments

Comments
 (0)