swiftlang/swift-java

Support Swift Exclusivity Enforcement

Opened this issue · 0 comments

We may need to start issuing swift_beginAccess and swift_endAccess when calling swift functions in order to allow the swift runtime to track and enforce potential exclusivity violations.

See also: https://www.swift.org/blog/swift-5-exclusivity/

Example:

To achieve memory safety, Swift requires exclusive access to a variable in order to modify that variable. In essence, a variable cannot be accessed via a different name for the duration in which the same variable is being modified as an inout argument or as self within a mutating method.

In the following example, count is accessed for modification by passing it as an inout argument. The exclusivity violation occurs because the modifier closure both reads the captured count variable and is called within the scope of the same variable’s modification. Inside the modifyTwice function, the count variable may only be safely accessed via the value inout argument, and within the modified closure it may only safely be accessed as $0.

func modifyTwice(_ value: inout Int, by modifier: (inout Int) -> ()) {
  modifier(&value)
  modifier(&value)
}