[StrictMemorySafety] Check the safety of return types of calls

Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
This commit is contained in:
Gabor Horvath
2025-08-04 17:28:20 +01:00
parent a47d39215d
commit 402ad33463
40 changed files with 126 additions and 98 deletions

View File

@@ -157,7 +157,7 @@ public func swift_slowAlloc(_ size: Int, _ alignMask: Int) -> UnsafeMutableRawPo
} else {
alignment = alignMask + 1
}
return alignedAlloc(size: size, alignment: alignment)
return unsafe alignedAlloc(size: size, alignment: alignment)
}
@_cdecl("swift_slowDealloc")
@@ -171,7 +171,7 @@ public func swift_allocObject(metadata: Builtin.RawPointer, requiredSize: Int, r
}
func swift_allocObject(metadata: UnsafeMutablePointer<ClassMetadata>, requiredSize: Int, requiredAlignmentMask: Int) -> UnsafeMutablePointer<HeapObject> {
let p = swift_slowAlloc(requiredSize, requiredAlignmentMask)!
let p = unsafe swift_slowAlloc(requiredSize, requiredAlignmentMask)!
let object = unsafe p.assumingMemoryBound(to: HeapObject.self)
unsafe _swift_embedded_set_heap_object_metadata_pointer(object, metadata)
unsafe object.pointee.refcount = 1