Skip to content

Commit 2de6442

Browse files
authored
Merge pull request #72785 from compnerd/error-storage
Runtime: add a non-Darwin error message storage
2 parents 4a27afa + 32a2dfc commit 2de6442

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

include/swift/Runtime/Debug.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ 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+
#if !defined(SWIFT_HAVE_CRASHREPORTERCLIENT)
131+
SWIFT_RUNTIME_EXPORT
132+
std::atomic<const char *> *swift_getFatalErrorMessageBuffer();
133+
#endif
134+
130135
// Halt due to an overflow in swift_retain().
131136
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
132137
void swift_abortRetainOverflow();

stdlib/public/runtime/Errors.cpp

Lines changed: 28 additions & 4 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"
@@ -64,14 +65,15 @@
6465
#include <inttypes.h>
6566

6667
#ifdef SWIFT_HAVE_CRASHREPORTERCLIENT
67-
#include <atomic>
6868
#include <malloc/malloc.h>
69-
70-
#include "swift/Runtime/Atomic.h"
69+
#else
70+
static std::atomic<const char *> kFatalErrorMessage;
7171
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
7272

7373
#include "BacktracePrivate.h"
7474

75+
#include <atomic>
76+
7577
namespace FatalErrorFlags {
7678
enum: uint32_t {
7779
ReportBacktrace = 1 << 0
@@ -297,7 +299,23 @@ reportOnCrash(uint32_t flags, const char *message)
297299
std::memory_order_release,
298300
SWIFT_MEMORY_ORDER_CONSUME));
299301
#else
300-
// empty
302+
const char *previous = nullptr;
303+
char *current = nullptr;
304+
previous =
305+
std::atomic_load_explicit(&kFatalErrorMessage, SWIFT_MEMORY_ORDER_CONSUME);
306+
307+
do {
308+
::free(current);
309+
current = nullptr;
310+
311+
if (previous)
312+
swift_asprintf(&current, "%s%s", current, message);
313+
else
314+
current = ::strdup(message);
315+
} while (!std::atomic_compare_exchange_strong_explicit(&kFatalErrorMessage,
316+
&previous, current,
317+
std::memory_order_release,
318+
SWIFT_MEMORY_ORDER_CONSUME));
301319
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
302320
}
303321

@@ -421,6 +439,12 @@ void swift::swift_reportWarning(uint32_t flags, const char *message) {
421439
warning(flags, "%s", message);
422440
}
423441

442+
#if !defined(SWIFT_HAVE_CRASHREPORTERCLIENT)
443+
std::atomic<const char *> *swift::swift_getFatalErrorMessageBuffer() {
444+
return &kFatalErrorMessage;
445+
}
446+
#endif
447+
424448
// Crash when a deleted method is called by accident.
425449
SWIFT_RUNTIME_EXPORT SWIFT_NORETURN void swift_deletedMethodError() {
426450
swift::fatalError(/* flags = */ 0,

0 commit comments

Comments
 (0)