Skip to content

Commit e773d9a

Browse files
authored
Merge pull request #70911 from stephentyrone/check-alignment-not-stride
2 parents b876662 + b8f5994 commit e773d9a

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ extension Unsafe${Mutable}RawBufferPointer {
793793
}
794794

795795
_debugPrecondition(
796-
Int(bitPattern: base) % MemoryLayout<S.Element>.stride == 0,
796+
Int(bitPattern: base) & (MemoryLayout<S.Element>.alignment-1) == 0,
797797
"buffer base address must be properly aligned to access S.Element"
798798
)
799799

@@ -853,7 +853,7 @@ extension Unsafe${Mutable}RawBufferPointer {
853853
return .init(start: nil, count: 0)
854854
}
855855
_debugPrecondition(
856-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
856+
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
857857
"buffer base address must be properly aligned to access C.Element"
858858
)
859859
_precondition(
@@ -878,7 +878,7 @@ extension Unsafe${Mutable}RawBufferPointer {
878878
}
879879
_internalInvariant(_end != nil)
880880
_debugPrecondition(
881-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
881+
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
882882
"buffer base address must be properly aligned to access C.Element"
883883
)
884884
var iterator = source.makeIterator()
@@ -940,7 +940,7 @@ extension Unsafe${Mutable}RawBufferPointer {
940940
return .init(start: nil, count: 0)
941941
}
942942
_debugPrecondition(
943-
Int(bitPattern: baseAddress) % MemoryLayout<T>.stride == 0,
943+
Int(bitPattern: baseAddress) & (MemoryLayout<T>.alignment-1) == 0,
944944
"buffer base address must be properly aligned to access T"
945945
)
946946
_precondition(

test/stdlib/UnsafeRawBufferPointer.swift

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ UnsafeRawBufferPointerTestSuite.test("nonmutating_subscript_setter") {
8383
}
8484
expectEqual(value2, value1)
8585

86-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 4, alignment: MemoryLayout<UInt>.alignment)
86+
let buffer = UnsafeMutableRawBufferPointer.allocate(
87+
byteCount: 4,
88+
alignment: MemoryLayout<UInt>.alignment
89+
)
8790
defer { buffer.deallocate() }
8891
buffer.copyBytes(from: [0, 1, 2, 3] as [UInt8])
8992
let leftBytes = buffer[0..<2]
@@ -131,7 +134,10 @@ UnsafeRawBufferPointerTestSuite.test("initFromArray") {
131134
#if !os(WASI)
132135
// Trap tests aren't available on WASI.
133136
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") {
134-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 30, alignment: MemoryLayout<UInt>.alignment)
137+
let buffer = UnsafeMutableRawBufferPointer.allocate(
138+
byteCount: 30,
139+
alignment: MemoryLayout<UInt64>.alignment
140+
)
135141
defer { buffer.deallocate() }
136142
let source = stride(from: 5 as Int64, to: 0, by: -1)
137143
if _isDebugAssertConfiguration() {
@@ -147,7 +153,10 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") {
147153
}
148154

149155
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") {
150-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 30, alignment: MemoryLayout<UInt>.alignment)
156+
let buffer = UnsafeMutableRawBufferPointer.allocate(
157+
byteCount: 30,
158+
alignment: MemoryLayout<UInt64>.alignment
159+
)
151160
defer { buffer.deallocate() }
152161
let source: [Int64] = [5, 4, 3, 2, 1]
153162
if _isDebugAssertConfiguration() {
@@ -164,7 +173,10 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") {
164173
#endif
165174

166175
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).exact") {
167-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 24, alignment: MemoryLayout<UInt>.alignment)
176+
let buffer = UnsafeMutableRawBufferPointer.allocate(
177+
byteCount: 3*MemoryLayout<Int64>.stride,
178+
alignment: MemoryLayout<Int64>.alignment
179+
)
168180
defer { buffer.deallocate() }
169181
let source: [Int64] = [5, 4, 3]
170182
var (it,bound) = buffer.initializeMemory(as: Int64.self, from: source)
@@ -213,7 +225,10 @@ UnsafeRawBufferPointerTestSuite.test("empty") {
213225
for _ in emptyBytes {
214226
expectUnreachable()
215227
}
216-
let emptyMutableBytes = UnsafeMutableRawBufferPointer.allocate(byteCount: 0, alignment: MemoryLayout<UInt>.alignment)
228+
let emptyMutableBytes = UnsafeMutableRawBufferPointer.allocate(
229+
byteCount: 0,
230+
alignment: MemoryLayout<UInt>.alignment
231+
)
217232
for _ in emptyMutableBytes {
218233
expectUnreachable()
219234
}
@@ -229,8 +244,10 @@ UnsafeRawBufferPointerTestSuite.test("reinterpret") {
229244
}
230245
let numPairs = 2
231246
let bytes = UnsafeMutableRawBufferPointer.allocate(
232-
byteCount: MemoryLayout<Pair>.stride * numPairs, alignment: MemoryLayout<UInt>.alignment)
233-
defer { bytes.deallocate() }
247+
byteCount: MemoryLayout<Pair>.stride * numPairs,
248+
alignment: MemoryLayout<Pair>.alignment
249+
)
250+
defer { bytes.deallocate() }
234251

235252
for i in 0..<(numPairs * 2) {
236253
bytes.storeBytes(of: Int32(i), toByteOffset: i * MemoryLayout<Int32>.stride,
@@ -259,7 +276,9 @@ UnsafeRawBufferPointerTestSuite.test("reinterpret") {
259276
UnsafeRawBufferPointerTestSuite.test("inBounds") {
260277
let numInts = 4
261278
let bytes = UnsafeMutableRawBufferPointer.allocate(
262-
byteCount: MemoryLayout<Int>.stride * numInts, alignment: MemoryLayout<UInt>.alignment)
279+
byteCount: MemoryLayout<Int>.stride * numInts,
280+
alignment: MemoryLayout<UInt>.alignment
281+
)
263282
defer { bytes.deallocate() }
264283

265284
for i in 0..<numInts {
@@ -289,7 +308,10 @@ UnsafeRawBufferPointerTestSuite.test("inBounds") {
289308
#if !os(WASI)
290309
// Trap tests aren't available on WASI.
291310
UnsafeRawBufferPointerTestSuite.test("subscript.get.underflow") {
292-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout<UInt>.alignment)
311+
let buffer = UnsafeMutableRawBufferPointer.allocate(
312+
byteCount: 2,
313+
alignment: MemoryLayout<UInt>.alignment
314+
)
293315
defer { buffer.deallocate() }
294316

295317
let bytes = UnsafeRawBufferPointer(rebasing: buffer[1..<2])
@@ -302,7 +324,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.get.underflow") {
302324
}
303325

304326
UnsafeRawBufferPointerTestSuite.test("subscript.get.overflow") {
305-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout<UInt>.alignment)
327+
let buffer = UnsafeMutableRawBufferPointer.allocate(
328+
byteCount: 2,
329+
alignment: MemoryLayout<UInt>.alignment
330+
)
306331
defer { buffer.deallocate() }
307332

308333
let bytes = UnsafeRawBufferPointer(rebasing: buffer[0..<1])
@@ -315,7 +340,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.get.overflow") {
315340
}
316341

317342
UnsafeRawBufferPointerTestSuite.test("subscript.set.underflow") {
318-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout<UInt>.alignment)
343+
let buffer = UnsafeMutableRawBufferPointer.allocate(
344+
byteCount: 2,
345+
alignment: MemoryLayout<UInt>.alignment
346+
)
319347
defer { buffer.deallocate() }
320348

321349
let bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[1..<2])
@@ -328,7 +356,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.set.underflow") {
328356
}
329357

330358
UnsafeRawBufferPointerTestSuite.test("subscript.set.overflow") {
331-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout<UInt>.alignment)
359+
let buffer = UnsafeMutableRawBufferPointer.allocate(
360+
byteCount: 2,
361+
alignment: MemoryLayout<UInt>.alignment
362+
)
332363
defer { buffer.deallocate() }
333364

334365
let bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[0..<1])
@@ -341,7 +372,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.set.overflow") {
341372
}
342373

343374
UnsafeRawBufferPointerTestSuite.test("subscript.range.get.underflow") {
344-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
375+
let buffer = UnsafeMutableRawBufferPointer.allocate(
376+
byteCount: 3,
377+
alignment: MemoryLayout<UInt>.alignment
378+
)
345379
defer { buffer.deallocate() }
346380

347381
let bytes = UnsafeRawBufferPointer(rebasing: buffer[1..<3])
@@ -354,7 +388,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.get.underflow") {
354388
}
355389

356390
UnsafeRawBufferPointerTestSuite.test("subscript.range.get.overflow") {
357-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
391+
let buffer = UnsafeMutableRawBufferPointer.allocate(
392+
byteCount: 3,
393+
alignment: MemoryLayout<UInt>.alignment
394+
)
358395
defer { buffer.deallocate() }
359396

360397
let bytes = UnsafeRawBufferPointer(rebasing: buffer[0..<2])
@@ -367,7 +404,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.get.overflow") {
367404
}
368405

369406
UnsafeRawBufferPointerTestSuite.test("subscript.range.set.underflow") {
370-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
407+
let buffer = UnsafeMutableRawBufferPointer.allocate(
408+
byteCount: 3,
409+
alignment: MemoryLayout<UInt>.alignment
410+
)
371411
defer { buffer.deallocate() }
372412

373413
let bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[1..<3])
@@ -380,7 +420,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.set.underflow") {
380420
}
381421

382422
UnsafeRawBufferPointerTestSuite.test("subscript.range.set.overflow") {
383-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
423+
let buffer = UnsafeMutableRawBufferPointer.allocate(
424+
byteCount: 3,
425+
alignment: MemoryLayout<UInt>.alignment
426+
)
384427
defer { buffer.deallocate() }
385428

386429
let bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[0..<2])
@@ -394,7 +437,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.set.overflow") {
394437
}
395438

396439
UnsafeRawBufferPointerTestSuite.test("subscript.range.narrow") {
397-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
440+
let buffer = UnsafeMutableRawBufferPointer.allocate(
441+
byteCount: 3,
442+
alignment: MemoryLayout<UInt>.alignment
443+
)
398444
defer { buffer.deallocate() }
399445

400446
if _isDebugAssertConfiguration() {
@@ -405,7 +451,10 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.narrow") {
405451
}
406452

407453
UnsafeRawBufferPointerTestSuite.test("subscript.range.wide") {
408-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
454+
let buffer = UnsafeMutableRawBufferPointer.allocate(
455+
byteCount: 3,
456+
alignment: MemoryLayout<UInt>.alignment
457+
)
409458
defer { buffer.deallocate() }
410459

411460
if _isDebugAssertConfiguration() {
@@ -431,7 +480,10 @@ UnsafeRawBufferPointerTestSuite.test("_copyContents") {
431480
#if !os(WASI)
432481
// Trap tests aren't available on WASI.
433482
UnsafeRawBufferPointerTestSuite.test("copyMemory.overflow") {
434-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
483+
let buffer = UnsafeMutableRawBufferPointer.allocate(
484+
byteCount: 3,
485+
alignment: MemoryLayout<UInt>.alignment
486+
)
435487
defer { buffer.deallocate() }
436488

437489
let bytes = buffer[0..<2]
@@ -461,7 +513,10 @@ UnsafeRawBufferPointerTestSuite.test("copyBytes.withoutContiguousStorage") {
461513
#if !os(WASI)
462514
// Trap tests aren't available on WASI.
463515
UnsafeRawBufferPointerTestSuite.test("copyBytes.sequence.overflow") {
464-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout<UInt>.alignment)
516+
let buffer = UnsafeMutableRawBufferPointer.allocate(
517+
byteCount: 3,
518+
alignment: MemoryLayout<UInt>.alignment
519+
)
465520
defer { buffer.deallocate() }
466521

467522
let bytes = buffer[0..<2]
@@ -650,7 +705,10 @@ UnsafeRawBufferPointerTestSuite.test("copy.sequence.overflow")
650705
#endif
651706

652707
UnsafeRawBufferPointerTestSuite.test("copy.overlap") {
653-
let bytes = UnsafeMutableRawBufferPointer.allocate(byteCount: 4, alignment: MemoryLayout<UInt>.alignment)
708+
let bytes = UnsafeMutableRawBufferPointer.allocate(
709+
byteCount: 4,
710+
alignment: MemoryLayout<UInt>.alignment
711+
)
654712
defer { bytes.deallocate() }
655713
// Right Overlap
656714
bytes[0] = 1

0 commit comments

Comments
 (0)