Files
swift-mirror/test/Interop/C/swiftify-import/counted-by.swift
Gabor Horvath 402ad33463 [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
2025-08-05 12:16:44 +01:00

168 lines
7.1 KiB
Swift

// REQUIRES: swift_feature_SafeInteropWrappers
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast | %FileCheck %s
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast %s
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __counted_by parameters.
import CountedByClang
// CHECK: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func binaryLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func bitshift(_ m: Int32, _ n: Int32, _ o: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func bitwise(_ m: Int32, _ n: Int32, _ o: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func complexExpr(_ len: Int{{.*}}, _ offset: Int{{.*}}, _ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func constFloatCastedToInt(_ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func constInt(_ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func hexLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nonnull(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nullUnspecified(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nullable(_ p: UnsafeMutableBufferPointer<Int{{.*}}>?)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func octalLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func offByOne(_ len: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func offBySome(_ len: Int32, _ offset: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableBufferPointer<Int{{.*}}>
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func scalar(_ m: Int32, _ n: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func simple(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func simpleFlipped(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func swiftAttr(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
@inlinable
public func callBitshift(_ m: CInt, n: CInt, o: CInt, _ p: UnsafeMutableBufferPointer<CInt>) {
unsafe bitshift(m, n, o, p)
}
@inlinable
public func callBitwise(_ m: CInt, n: CInt, o: CInt, _ p: UnsafeMutableBufferPointer<CInt>) {
unsafe bitwise(m, n, o, p)
}
@inlinable
public func callComplexExpr(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe complexExpr(CInt(p.count), 1, p)
}
@inlinable
public func callConstFloatCastedToInt(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe constFloatCastedToInt(p)
}
@inlinable
public func callConstInt(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe constInt(p)
}
@inlinable
public func callNonnull(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe nonnull(p)
}
@inlinable
public func callNullUnspecified(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe nullUnspecified(p)
}
@inlinable
public func callNullable(_ p: UnsafeMutableBufferPointer<CInt>?) {
unsafe nullable(p)
}
@inlinable
public func callOctalLiteral(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe octalLiteral(p)
}
@inlinable
public func callOffByOne(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe offByOne(0, p)
}
@inlinable
public func callOffBySome(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe offBySome(0, 1, p)
}
@inlinable
public func callReturnPointer() {
let _: UnsafeMutableBufferPointer<CInt>? = unsafe returnPointer(4) // call wrapper
let _: UnsafeMutablePointer<CInt>? = unsafe returnPointer(4) // call unsafe interop
}
@inlinable
public func callScalar(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe scalar(4, 2, p)
}
@inlinable
public func callShared(_ p: UnsafeMutableBufferPointer<CInt>, _ p2: UnsafeMutableBufferPointer<CInt>) {
unsafe shared(p, p2)
}
@inlinable
public func callSimple(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe simple(p)
}
@inlinable
public func callSimpleIndirectOriginal(_ p: UnsafeMutablePointer<CInt>) {
let f = unsafe simple
unsafe f(13, p)
}
@inlinable
public func callSimpleIndirectOverload(_ p: UnsafeMutableBufferPointer<CInt>) {
let f: (UnsafeMutableBufferPointer<CInt>) -> Void = unsafe simple
unsafe f(p)
}
@inlinable
public func callSimpleFlipped(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe simpleFlipped(p)
}
@inlinable
public func callSwiftAttr(_ p: UnsafeMutableBufferPointer<CInt>) {
unsafe swiftAttr(p)
}