Skip to content

Commit 7e168ab

Browse files
committed
Fix potential memory corruption from CachedCompilation.Iterator
The new experimental compiler feature `CheckImplementationOnly` enables type-checking the use of `@_implementationOnly` imports when building without library-evolution. I tried enabling this check on the driver project and it reported one potential issue that I hope to fix here. The issue is a property in a struct with the static type of `CSwiftScan.swiftscan_cached_compilation_t` where `CSwiftScan` is `@_implementationOnly` imported. This means that clients using this structs wouldn't see the right memory layout information unless they import `CSwiftScan` too. This may not have been an issue in practice if the type was used only within the same module. I'm suggesting a fix here. Keeping a reference to `CachedCompilation` should be safe as it's a class that hides the memory layout from the clients. We should adopt `CheckImplementationOnly` in the driver once it's completed, some early versions of it were too strict.
1 parent 97ab531 commit 7e168ab

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ extension CachedCompilation: Sequence {
7878
public struct Iterator: IteratorProtocol {
7979
public typealias Element = CachedOutput
8080
let limit: UInt32
81-
let ptr: swiftscan_cached_compilation_t
82-
let lib: SwiftScan
81+
let sequence: CachedCompilation
8382
var idx: UInt32 = 0
8483
public mutating func next() -> CachedOutput? {
8584
guard idx < self.limit else { return nil }
86-
let output = self.lib.api.swiftscan_cached_compilation_get_output(self.ptr, idx)
85+
let output = self.sequence.lib.api.swiftscan_cached_compilation_get_output(self.sequence.ptr, idx)
8786
idx += 1
8887
// output can never be nil.
89-
return CachedOutput(output!, lib: self.lib)
88+
return CachedOutput(output!, lib: self.sequence.lib)
9089
}
9190
}
9291
public func makeIterator() -> Iterator {
93-
return Iterator(limit: self.count, ptr: self.ptr, lib: self.lib)
92+
return Iterator(limit: self.count, sequence: self)
9493
}
9594
}
9695

0 commit comments

Comments
 (0)