Skip to content

Commit 5f5d1cf

Browse files
authored
Merge pull request #80119 from mikeash/allocation-failure-fatal-error
[Runtime] Include size/alignment in allocation failure fatal error message.
2 parents 521e2f6 + 953a7a5 commit 5f5d1cf

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

include/swift/Runtime/Debug.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ void swift_abortDynamicReplacementDisabling();
160160
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
161161
void swift_abortDisabledUnicodeSupport();
162162

163+
// Halt due to a failure to allocate memory.
164+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
165+
void swift_abortAllocationFailure(size_t size, size_t alignMask);
166+
163167
/// This function dumps one line of a stack trace. It is assumed that \p framePC
164168
/// is the address of the stack frame at index \p index. If \p shortOutput is
165169
/// true, this functions prints only the name of the symbol and offset, ignores

stdlib/public/runtime/Errors.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,23 @@ SWIFT_RUNTIME_EXPORT SWIFT_NORETURN void swift_deletedMethodError() {
481481
// FIXME: can't pass the object's address from InlineRefCounts without hacks
482482
void swift::swift_abortRetainOverflow() {
483483
swift::fatalError(FatalErrorFlags::ReportBacktrace,
484-
"Fatal error: Object was retained too many times");
484+
"Fatal error: Object was retained too many times\n");
485485
}
486486

487487
// Crash due to an unowned retain count overflow.
488488
// FIXME: can't pass the object's address from InlineRefCounts without hacks
489489
void swift::swift_abortUnownedRetainOverflow() {
490490
swift::fatalError(FatalErrorFlags::ReportBacktrace,
491-
"Fatal error: Object's unowned reference was retained too many times");
491+
"Fatal error: Object's unowned reference was retained too "
492+
"many times\n");
492493
}
493494

494495
// Crash due to a weak retain count overflow.
495496
// FIXME: can't pass the object's address from InlineRefCounts without hacks
496497
void swift::swift_abortWeakRetainOverflow() {
497498
swift::fatalError(FatalErrorFlags::ReportBacktrace,
498-
"Fatal error: Object's weak reference was retained too many times");
499+
"Fatal error: Object's weak reference was retained too "
500+
"many times\n");
499501
}
500502

501503
// Crash due to retain of a dead unowned reference.
@@ -504,32 +506,40 @@ void swift::swift_abortRetainUnowned(const void *object) {
504506
if (object) {
505507
swift::fatalError(FatalErrorFlags::ReportBacktrace,
506508
"Fatal error: Attempted to read an unowned reference but "
507-
"object %p was already deallocated", object);
509+
"object %p was already deallocated\n", object);
508510
} else {
509511
swift::fatalError(FatalErrorFlags::ReportBacktrace,
510512
"Fatal error: Attempted to read an unowned reference but "
511-
"the object was already deallocated");
513+
"the object was already deallocated\n");
512514
}
513515
}
514516

515517
/// Halt due to enabling an already enabled dynamic replacement().
516518
void swift::swift_abortDynamicReplacementEnabling() {
517519
swift::fatalError(FatalErrorFlags::ReportBacktrace,
518520
"Fatal error: trying to enable a dynamic replacement "
519-
"that is already enabled");
521+
"that is already enabled\n");
520522
}
521523

522524
/// Halt due to disabling an already disabled dynamic replacement().
523525
void swift::swift_abortDynamicReplacementDisabling() {
524526
swift::fatalError(FatalErrorFlags::ReportBacktrace,
525527
"Fatal error: trying to disable a dynamic replacement "
526-
"that is already disabled");
528+
"that is already disabled\n");
529+
}
530+
531+
/// Halt due to a failure to allocate memory.
532+
void swift::swift_abortAllocationFailure(size_t size, size_t alignMask) {
533+
swift::fatalError(FatalErrorFlags::ReportBacktrace,
534+
"Fatal error: failed to allocate %zu bytes of memory with "
535+
"alignment %zu\n", size, alignMask + 1);
527536
}
528537

529538
/// Halt due to trying to use unicode data on platforms that don't have it.
530539
void swift::swift_abortDisabledUnicodeSupport() {
531540
swift::fatalError(FatalErrorFlags::ReportBacktrace,
532-
"Unicode normalization data is disabled on this platform");
541+
"Unicode normalization data is disabled on this "
542+
"platform\n");
533543

534544
}
535545

stdlib/public/runtime/Heap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
9191
size_t alignment = computeAlignment(alignMask);
9292
p = AlignedAlloc(size, alignment);
9393
}
94-
if (!p) swift::crash("Could not allocate memory.");
94+
if (!p) swift::swift_abortAllocationFailure(size, alignMask);
9595
return p;
9696
}
9797

@@ -113,7 +113,7 @@ void *swift::swift_slowAllocTyped(size_t size, size_t alignMask,
113113
if (err != 0)
114114
p = nullptr;
115115
}
116-
if (!p) swift::crash("Could not allocate memory.");
116+
if (!p) swift::swift_abortAllocationFailure(size, alignMask);
117117
return p;
118118
}
119119
#endif
@@ -125,7 +125,7 @@ void *swift::swift_coroFrameAlloc(size_t size,
125125
#if SWIFT_STDLIB_HAS_MALLOC_TYPE
126126
if (__builtin_available(macOS 15, iOS 17, tvOS 17, watchOS 10, *)) {
127127
void *p = malloc_type_malloc(size, typeId);
128-
if (!p) swift::crash("Could not allocate memory.");
128+
if (!p) swift::swift_abortAllocationFailure(size, 0);
129129
return p;
130130
}
131131
#endif

0 commit comments

Comments
 (0)