Skip to content

Commit 7a6b8d1

Browse files
author
Kyle Macomber
authored
Merge pull request #38452 from glessard/absurd-allocations-5.5
[5.5][runtime] Improve detection of memory allocation failures
2 parents 22dd46f + f28f492 commit 7a6b8d1

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

include/swift/Basic/Malloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ inline void *AlignedAlloc(size_t size, size_t align) {
3434
if (align < sizeof(void*))
3535
align = sizeof(void*);
3636

37-
void *r;
3837
#if defined(_WIN32)
39-
r = _aligned_malloc(size, align);
38+
void *r = _aligned_malloc(size, align);
4039
assert(r && "_aligned_malloc failed");
4140
#else
41+
void *r = nullptr;
4242
int res = posix_memalign(&r, align, size);
4343
assert(res == 0 && "posix_memalign failed");
4444
(void)res; // Silence the unused variable warning.

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,21 @@ extension Unsafe${Mutable}RawBufferPointer: RandomAccessCollection { }
263263

264264
extension Unsafe${Mutable}RawBufferPointer {
265265
% if mutable:
266-
/// Returns a newly allocated buffer with the given size, in bytes.
266+
/// Allocates uninitialized memory with the specified size and alignment.
267267
///
268-
/// The memory referenced by the new buffer is allocated, but not
269-
/// initialized.
268+
/// You are in charge of managing the allocated memory. Be sure to deallocate
269+
/// any memory that you manually allocate.
270+
///
271+
/// The allocated memory is not bound to any specific type and must be bound
272+
/// before performing any typed operations. If you are using the memory for
273+
/// a specific type, allocate memory using the
274+
/// `UnsafeMutablePointerBuffer.allocate(capacity:)` static method instead.
270275
///
271276
/// - Parameters:
272-
/// - byteCount: The number of bytes to allocate.
277+
/// - byteCount: The number of bytes to allocate. `byteCount` must not be
278+
/// negative.
273279
/// - alignment: The alignment of the new region of allocated memory, in
274-
/// bytes.
280+
/// bytes. `alignment` must be a whole power of 2.
275281
/// - Returns: A buffer pointer to a newly allocated region of memory aligned
276282
/// to `alignment`.
277283
@inlinable

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public struct UnsafeMutableRawPointer: _Pointer, Sendable {
600600
/// - Parameters:
601601
/// - byteCount: The number of bytes to allocate. `byteCount` must not be negative.
602602
/// - alignment: The alignment of the new region of allocated memory, in
603-
/// bytes.
603+
/// bytes. `alignment` must be a whole power of 2.
604604
/// - Returns: A pointer to a newly allocated region of memory. The memory is
605605
/// allocated, but not initialized.
606606
@inlinable

test/stdlib/UnsafeRawPointer.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,18 @@ UnsafeMutableRawPointerExtraTestSuite.test("moveInitialize:from:") {
229229
check(Check.RightOverlap)
230230
}
231231

232+
UnsafeMutableRawPointerExtraTestSuite.test("absurd.allocation.misaligned") {
233+
expectCrashLater()
234+
let mustFail = UnsafeMutableRawPointer.allocate(byteCount: 1024,
235+
alignment: 19)
236+
expectUnreachable()
237+
}
238+
239+
UnsafeMutableRawPointerExtraTestSuite.test("absurd.allocation.gargantuan") {
240+
expectCrashLater()
241+
let mustFail = UnsafeMutableRawPointer.allocate(byteCount: Int.max,
242+
alignment: 0)
243+
expectUnreachable()
244+
}
245+
232246
runAllTests()

0 commit comments

Comments
 (0)