Skip to content

Commit aa54818

Browse files
authored
Merge pull request #39976 from apple/jgrynspan/temporary-allocation-compile-time-error-for-32-bit-overflow
On targets smaller than 64 bits, emit a compile-time error if withUnsafeTemporaryAllocation(of:capacity:) byte count overflows
2 parents 7349283 + 3ab0ae2 commit aa54818

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,14 @@ static llvm::Value *getStackAllocationSize(IRGenSILFunction &IGF,
30293029
auto overflow = llvm::MulOverflow(*capacityValue, *strideValue, byteCount);
30303030
if (overflow) {
30313031
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
3032+
} else {
3033+
// For architectures narrower than 64 bits, check if the byte count fits
3034+
// in a (signed) size value.
3035+
auto maxByteCount = llvm::APInt::getSignedMaxValue(
3036+
IGF.IGM.SizeTy->getBitWidth()).getSExtValue();
3037+
if (byteCount > maxByteCount) {
3038+
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
3039+
}
30323040
}
30333041
result = llvm::ConstantInt::get(IGF.IGM.SizeTy, byteCount);
30343042

test/IRGen/temporary_allocation/bad_constants.swift renamed to test/IRGen/temporary_allocation/size_too_large.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@_silgen_name("blackHole")
44
func blackHole(_ value: UnsafeMutableRawPointer?) -> Void
55

6-
withUnsafeTemporaryAllocation(byteCount: 1, alignment: -1) { buffer in
6+
withUnsafeTemporaryAllocation(of: Int.self, capacity: .max) { buffer in
77
blackHole(buffer.baseAddress)
88
}
9-
// CHECK: error: alignment value must be greater than zero
9+
// CHECK: error: allocation byte count too large

0 commit comments

Comments
 (0)